Manually plot spectrogram: "Surf" the output of the spectrogram function

176 次查看(过去 30 天)
Hello, I try to understand the workings of the spectrogram function by reproducing the same plot that the spectrogram function gives by using the output parameters of the spectrogram function. Also I try to understand the difference between Power Spectral Density and Power Spectrum, which are two optional return values of the spectrogram function. Below you can see my code and a picture that shows the output.
% Try to understand what the spectrogram is doing and how to manually plot
% the spectrogram and also what PSD vs power spectrum is
clc; clear; close all;
win_size = 0.01;
fft_overlap = 0.5;
[signal, Fs] = audioread('../RASTA/speech_5.wav');
signal = signal(:,1); %use only the left channel
hop_size = Fs*win_size;
nfft = hop_size/fft_overlap;
noverlap = nfft-hop_size;
w = sqrt(hann(nfft)); %use some window
%Normal Spectrogram plot
subplot(4,1,1);
spectrogram(signal, w ,noverlap, nfft, Fs, 'yaxis' );
colormap jet;
title('Default spectrogram plot');
%Try to plot the spectrogram from the output
[s, f, t] = spectrogram(signal, w ,noverlap, nfft, Fs);
subplot(4,1,2);
surf(t, f, 20*log10(abs(s)), 'EdgeColor', 'none');
axis xy;
axis tight;
colormap(jet); view(0,90);
xlabel('Time (secs)');
colorbar;
ylabel('Frequency(HZ)');
title('surf(t, f, 20*log10(abs(s)), ___)');
%why is the spectrum "cut" , there are no zero values displayed at the
%beginning and at the end. Also it seems different from the normal
%spectrogram plot (more red and fewer blue)
%Try to utilize the ps output with 'psd' argument
[s, f, t, psd] = spectrogram(signal, w ,noverlap, nfft, Fs, 'psd');
subplot(4,1,3);
surf(t, f, psd, 'EdgeColor', 'none');
axis xy;
axis tight;
colormap(jet); view(0,90);
xlabel('Time');
colorbar;
ylabel('Frequency(HZ)');
title('surf(t, f, psd, ___)');
%Its only blue (meaning very small values) but since its the psd why should
%I need to do 10*log10(psd) on it?
%Try to utilize the ps output with 'power' argument
[s, f, t, power] = spectrogram(signal, w ,noverlap, nfft, Fs, 'power');
subplot(4,1,4);
surf(t, f, power, 'EdgeColor', 'none');
axis xy;
axis tight;
colormap(jet); view(0,90);
xlabel('Time');
colorbar;
ylabel('Frequency(HZ)');
title('surf(t, f, power, ___)');
%Whats the difference between the variables power and psd? Spectrum seems
%to be identical..
Please help me to educate myself on the workings of this function and on spectrograms, PSD and Power Spectrum in general. How do I have to fix my code to produce identical plots?

回答(4 个)

Sebastian Schneider
For your color mapping try to set your lower colorlimit to 80%:
figure
surf(t,f,10*log10(psd),'EdgeColor','none');
colormap jet
ax=gca;
colorlim = get(ax,'clim');
newlim = [(colorlim(1)*0.8),colorlim(2)];
set(ax,'clim',(newlim));

Francisco Flores
Francisco Flores 2022-6-29
编辑:Francisco Flores 2022-6-29
Hi all,
To plot the output of the spectrogram signal do the following (example with a toy signal):
fs = 1000;
t = 0 : 1 / fs : 2;
x = chirp( t, 100, 1, 200, 'quadratic' );
[ X, f, t, S ] = spectrogram( x, 128, 120, 128, fs );
imagesc( t, f, pow2db( S ) )
axis xy
xlabel( 'time (s)' )
ylabel( 'frequency (Hz)' )
I'm converting the power to decibels, because typically that is the most helpful for visualization.

Leore Heim
Leore Heim 2019-12-26
Dear Joschua,
Have you found an asnwer to your question?
I too am puzzled as to the difference between the 'power' and 'psd' arugemnts.

Pascal Stirtzel
Pascal Stirtzel 2022-1-24
Does Anyone can help us here. I have the same Problem and dont find a way how to understand how to get from the output of the function to the plot

类别

Help CenterFile Exchange 中查找有关 Time-Frequency Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by