How to set Spectrogram parameters
Afficher commentaires plus anciens
Hello Everyone,
I would like to print the spectrogram of a curve, in order to observe variations. But I don't really understand how to set the parameters of the function "spectrogram" even with the Matlab help.
For now I have this :

But I can't observe any variation. Is it possible to obtain something more like that :

There is my code and the attached file to get all the datas :
clear all;
clc ;
close all;
Valeurs = xlsread('C:\Users\tlam\Desktop\Run 80.xlsx',"Data1");
Temps = Valeurs(:,1);
Accelero = Valeurs(:,2);
Moteur_rpm = Valeurs(:,3);
BV_rpm = Valeurs(:,4);
Delta = Moteur_rpm - BV_rpm;
M_temps = max(Temps);
Moteur_Hz = Moteur_rpm * 0.016667;
BV_Hz = BV_rpm * 0.016667 ;
Delta_Hz = Delta * 0.016667;
%%
F = figure( 'Position', [50 50 1600 900]) ; % Visible 'off' pour ne pas afficher
t = tiledlayout(3,3,'TileSpacing','Compact','Padding','Compact');
title(t,"Spectro FULL ")
% Mot_rpm(t) et BV_rpm(t) Run 1
nexttile([1 2])
plot(Temps,Moteur_rpm,'red',Temps,BV_rpm,'green')
xlim ([0 M_temps])
ylabel('Moteur & BV [rpm]')
legend ('Moteur','Boite de Vitesse')
grid on
Fs = 50000; %Hz
% Spectrogramme Run 1
nexttile([2 1])
spectrogram(Accelero,100,99,100,Fs,'yaxis','power')
hold on
plot(Temps,Moteur_Hz,'r',Temps,BV_Hz,'g',Temps,Delta_Hz,'b')
colormap('jet')
% Delta(t) Run 1
nexttile([1 2])
plot(Temps,Delta,'blue')
xlim ([0 M_temps])
ylabel('Delta [rpm]')
grid on
% Accelero(t) Run 1
nexttile([1 2])
plot(Temps,Accelero,'magenta')
xlabel('temps [s]')
xlim ([0 M_temps])
ylabel('acceleromètre [V]')
grid on
19 commentaires
Mathieu NOE
le 9 Mai 2023
Hello
I played a bit with your code and data and tried to make a better spectrogram rendering
I added some high pass filetring on your accel data, otherwise the spectrogram has a very strong output at DC 'not very interesting !)

now you can play with NFFT and OVERLAP if you want to have different frequency and time resolution
also I noticed that inside your code you hardcoded Fs = 5000, whereas from the dat file it's 50 Hz, what is the correct value ?
I could not find any visual correlation between your accel spectrogram and the other signals (converted to Hz) but I wonder if it's correct to compare this way as it assumes that you are only looking at the first engine order (when you convert from RPM to Hz).
For an IC engine, the vibration are higher at multiple engine orders (2nd and above typically)
code a bit tweaked :
clear all;
clc ;
close all;
% Valeurs = xlsread('C:\Users\tlam\Desktop\Run 80.xlsx',"Data1");
Valeurs = xlsread('Run 1.xlsx',"Data1");
Temps = Valeurs(:,1);
dt = mean(diff(Temps));
Fs = 1/dt;
Accelero = Valeurs(:,2);
Moteur_rpm = Valeurs(:,3);
BV_rpm = Valeurs(:,4);
Delta = Moteur_rpm - BV_rpm;
M_temps = max(Temps);
Moteur_Hz = Moteur_rpm /60;
BV_Hz = BV_rpm /60 ;
Delta_Hz = Delta /60;
% high pass filtering of accel signal
f_high = 1; % Hz
order = 3
[b,a] = butter(3,2/Fs*f_high,'high');
Accelero_filtered = filtfilt(b,a,Accelero);
%%
% F = figure( 'Position', [50 50 1600 900]) ; % Visible 'off' pour ne pas afficher
F = figure() ; % Visible 'off' pour ne pas afficher
t = tiledlayout(3,3,'TileSpacing','Compact','Padding','Compact');
title(t,"Spectro FULL ")
% Mot_rpm(t) et BV_rpm(t) Run 1
nexttile([1 2])
plot(Temps,Moteur_rpm,'red',Temps,BV_rpm,'green')
xlim ([0 M_temps])
ylabel('Moteur & BV [rpm]')
legend ('Moteur','Boite de Vitesse')
grid on
%Fs = 50000; %Hz ???
% Spectrogramme Run 1
nexttile([2 1])
NFFT =200;
NOVERLAP = round(0.95*NFFT);
spectrogram(Accelero_filtered,hanning(NFFT),NOVERLAP,NFFT,Fs,'yaxis','power','MinThreshold',-80)
hold on
plot(Temps,Moteur_Hz,'r',Temps,BV_Hz,'g',Temps,Delta_Hz,'b')
colormap('jet')
% Delta(t) Run 1
nexttile([1 2])
plot(Temps,Delta,'blue')
xlim ([0 M_temps])
ylabel('Delta [rpm]')
grid on
% Accelero(t) Run 1
nexttile([1 2])
plot(Temps,Accelero,'magenta')
xlabel('temps [s]')
xlim ([0 M_temps])
ylabel('acceleromètre [V]')
grid on
Théo
le 9 Mai 2023
Théo
le 9 Mai 2023
Mathieu NOE
le 9 Mai 2023
as you increase the sampling rate , the frequency resolution df will decrease if you keep NFFT the same (because df = Fs/NFFT)
so you may have to increase it significantly
now for overlap , i rarely go above 75% of overlap if the transients are not super brutal (and you need a refined time resolution) , so you shouldn't have to change it all the time
Théo
le 9 Mai 2023
Mathieu NOE
le 9 Mai 2023
try also to shift the high pass filter cut off frequency to a higher value (~ 1 kHz ?)
Mathieu NOE
le 9 Mai 2023
I'm curious
why did you apply a factor 1000 on the green and red frequency curves (that is like you are plotinng engine 1000th order frequency ?)
Mathieu NOE
le 9 Mai 2023
which brings me another remark from your spectrogram , as the energy sems to be vanishing above 1 kHz
what frequency range are you indeed looking for ?
Théo
le 9 Mai 2023
Mathieu NOE
le 9 Mai 2023
I am a bit lost why you have either 50 Hz or 50 kHz sampling rate
are you measuring two distinct engines (one slow and one fast) ? or are you measuring the same engine with two different recording parameters ?
I just don't get why in the second case (Fs = 50 kHz), on the spectrogram , the green, blue and red curves shows values in the kHz range , whereas the RPM plots (data) seems the same as in the first case (Fs = 50 Hz)
I'm talking about these data :
Moteur_Hz = Moteur_rpm /60;
BV_Hz = BV_rpm /60 ;
Delta_Hz = Delta /60;
anyway it seems the spectrogram shows no energy that is close more or less to the 3 curves (only background noise)
Théo
le 10 Mai 2023
Mathieu NOE
le 10 Mai 2023
Modifié(e) : Mathieu NOE
le 10 Mai 2023
can you share your last version of the code with an extract of the 50 kHz data ? I don't need the full length of data
Théo
le 10 Mai 2023
Mathieu NOE
le 10 Mai 2023
hello
try this new code
I let you discover the new spectrogram (with frequency axis in log scale to have good rendering both low and high freqs)
now the engine frequency trace is to be seen

by again I see no special event that coincide with the engine freq line
clear all;
clc ;
close all;
Valeurs = xlsread('Run 80bis.xlsx',"Data1");
Temps = Valeurs(:,1);
Accelero = Valeurs(:,2);
Moteur_rpm = Valeurs(:,3);
BV_rpm = Valeurs(:,4);
Delta = Moteur_rpm - BV_rpm;
M_temps = max(Temps);
Moteur_Hz = Moteur_rpm /60;
BV_Hz = BV_rpm /60 ;
Delta_Hz = Delta /60;
dt = mean(diff(Temps));
Fs = 1/dt; %Hz
% high pass filtering of accel signal
f_high = 5; % Hz
order = 2;
[b,a] = butter(3,2/Fs*f_high,'high');
Accelero_filtered = filtfilt(b,a,Accelero);
%%
F = figure( 'Position', [50 50 1600 900]) ; % Visible 'off' pour ne pas afficher
t = tiledlayout(3,3,'TileSpacing','Compact','Padding','Compact');
title(t,"Spectro FULL ")
% Mot_rpm(t) et BV_rpm(t) Run 1
nexttile([1 2])
plot(Temps,Moteur_rpm,'red',Temps,BV_rpm,'green')
xlim ([0 M_temps])
ylabel('Moteur & BV [rpm]')
legend ('Moteur','Boite de Vitesse')
grid on
nexttile([2 1])
NFFT =20000;
NOVERLAP = round(0.95*NFFT);
[S,F,T] = spectrogram(Accelero_filtered,hanning(NFFT),NOVERLAP,NFFT,Fs,'yaxis','power');
idf = (F>0); % remove zero (dc) freq
F = F(idf);
S = S(idf,:);
SdB = 20*log10(abs(S)); % convert S in dB
SdBmax = max(SdB,[],'all');
SdB_range = 80; % range in dB to display
SdB(SdB<SdBmax-SdB_range) = NaN; % remove data below threshold (corresponding to max value - SdB_range)
imagesc(T,F,SdB);
set(gca, 'YDir','normal'); % to have the y = 0 at the bottom and not at the top
set(gca, 'YScale','log'); %
colorbar('vert');
xlabel('temps [s]')
xlim ([0 M_temps])
ylabel('Freq (Hz)')
hold on
plot(Temps,Moteur_Hz,'r',Temps,BV_Hz,'g',Temps,Delta_Hz,'b')
colormap('jet')
% Delta(t) Run 1
nexttile([1 2])
plot(Temps,Delta,'blue')
xlim ([0 M_temps])
ylabel('Delta [rpm]')
grid on
% Accelero(t) Run 1
nexttile([1 2])
plot(Temps,Accelero,'magenta')
xlabel('temps [s]')
xlim ([0 M_temps])
ylabel('acceleromètre [V]')
grid on
Mathieu NOE
le 11 Mai 2023
to you last question :
yes, the spectrogram max displayed frequency is Fs / 2 = 25 Hz
so any RPM trace above 25*60 = 1500 RPM will not appear
all the best for the future !
Théo
le 11 Mai 2023
Mathieu NOE
le 11 Mai 2023
My pleasure !
Réponses (0)
Catégories
En savoir plus sur Parametric Spectral Estimation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







