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

108 vues (au cours des 30 derniers jours)
Joschua Kraus
Joschua Kraus le 9 Mai 2018
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?

Réponses (4)

Sebastian Schneider
Sebastian Schneider le 3 Mai 2019
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 le 29 Juin 2022
Modifié(e) : Francisco Flores le 29 Juin 2022
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 le 26 Déc 2019
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 le 24 Jan 2022
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

Catégories

En savoir plus sur Time-Frequency Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by