Different outputs from spectrogram and pwelch

21 views (last 30 days)
When analysing an acoustic signal, I would expect to get the same results presented in a spectrogram or a PSD analysis, when using the same input variables.
However, this is not the case with my example data (xbit.mat), attached.
%Read audio file
load('xbit.mat');
Fs=96000; %sample rate
xbit=detrend(xbit);
cal=-163.287; %calibration value
% FFT inputs
window=Fs;
nfft=Fs;
overlap=Fs/2;
% Spectrogram
Calibration=10^((abs(cal))/20); % calibration value for specific device used to record data
xbit_cal=xbit*Calibration;
figure(1)
subplot(2,1,1)
[S,F,T,P] = spectrogram(xbit_cal,window,overlap,nfft,Fs,'yaxis');
surf(T,F,10*log10(P),'edgecolor','none');
colorbar;
view(0,90);
ylim([50 24000])
set(gca,'YScale','log','TickDir','out');
clim([0 120]);
ylabel('Frequency');
xlabel('Time')
ylabel(colorbar,'Intensity','VerticalAlignment','bottom','Rotation',270,'FontSize',12)
% PSD analysis
[pxx,f]=pwelch(xbit,window,overlap,nfft,fs); %perform PSD analysis
%signal, window/segment length, overlap, nfft
pxx_dB=10*log10(pxx)-cal; %convert to dB re 1µPa and calibrate
subplot(2,1,2)
semilogx(f,pxx_dB)
ylabel('Intensity')
xlabel('Frequency')

Accepted Answer

Louise Wilson
Louise Wilson on 14 Aug 2023
There was a mistake in my code which I have now rectified. The results for the spectrogram and spectra now match.

More Answers (1)

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 11 Aug 2023
  1. Spectrogram Scaling: In your spectrogram analysis, you are converting the spectrogram power values to dB and applying calibration (cal) using P=((10*log10(P))-cal);. This could lead to the differences in the value ranges between the spectrogram and PSD.
  2. PSD Scaling: In your PSD analysis, you are calculating the PSD values using pwelch, which already provides the power spectral density values in dB. However, you are applying an additional calibration (pxx_dB=10*log10(pxx)-cal;) to the PSD values. This might be causing the PSD values to be shifted.
To maintain consistency and facilitate a direct comparison between the spectrogram and PSD, you should avoid applying additional calibrations and dB conversions to both the spectrogram and PSD values. Here's how you can modify your code:
% ... (previous code)
% Spectrogram
figure(1)
subplot(2,1,1)
[S,F,T,P] = spectrogram(xbit,window,overlap,nfft,Fs,'yaxis');
surf(T,F,P,'edgecolor','none');
colorbar;
view(0,90);
ylim([50 24000])
set(gca,'YScale','log','TickDir','out');
clim([0 120]);
ylabel('Frequency');
xlabel('Time')
ylabel(colorbar,'Intensity','VerticalAlignment','bottom','Rotation',270,'FontSize',12)
% PSD analysis
pxx=pwelch(xbit,window,overlap,nfft); %perform PSD analysis
pxx_dB=10*log10(pxx); % convert to dB re 1µPa
subplot(2,1,2)
plot(pxx_dB)
ylabel('Intensity')
xlabel('Frequency')
By removing the cal calibration and dB conversions from both the spectrogram and PSD calculations, you should get a more accurate representation of the value ranges between the two analyses. This will allow you to compare the spectrogram and PSD more directly.
  1 Comment
Louise Wilson
Louise Wilson on 11 Aug 2023
Thanks for your answer!
This does not help me though because I need to apply the calibration value for the values to be valid?
Surely there is a solution to apply it consistently to both outputs, which should be done once they are converted to dB? (since the calibration value is in dB)

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!