Improve STFT Plot Clarity

14 views (last 30 days)
Abo
Abo on 22 Apr 2025
Commented: Mathieu NOE on 22 Apr 2025
Hi,
I'm analyzing a time-domain signal using STFT and want to improve the clarity of the spectrogram. Here's my code:
load('D.mat')
nfft_custom = 1024*5;
wlen = nfft_custom;
win = hann(wlen, 'periodic');
[S, f, t_stft] = stft(AAA, 100000, ...
'Window', win, ...
'OverlapLength', round(0.5 * wlen), ...
'FFTLength', nfft_custom);
stft_dB = 20*log10(abs(S) + eps);
imagesc(t_stft, f, stft_dB); axis xy; ylim([0 800]); caxis([-70 0]);
xlabel('Time (s)'); ylabel('Frequency (Hz)');

Accepted Answer

Mathieu NOE
Mathieu NOE on 22 Apr 2025
hello @Abo
and welcome back !
fist idea with STFT is to maximize the overlap , but anyway the STFT has not the best performance when you look for time AND frequency accuracy (cwt would be a better choice : look at the example in : CWT-Based Time-Frequency Analysis
to reduce the amount of fft computation I decided to downsample the data first with decimate (otherwise you are just doing a lot of computations and throw 90% at the garbage bin
to further improve the rendering with imagesc you can use the option : 'Interpolation', 'bilinear'
load('D.mat')
Fs = 100000;
% decimate the data
r = 8;
AAA =decimate(AAA,r);
size(AAA)
ans = 1×2
4096 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Fs = Fs/r
Fs = 12500
%% spectrogram
nfft_custom = 1024;
wlen = nfft_custom;
win = hann(wlen, 'periodic');
[S, f, t_stft] = stft(AAA, Fs,'Window', win,'OverlapLength', round(0.95 * wlen), ...
'FFTLength', nfft_custom,'FrequencyRange',"onesided");
stft_dB = 20*log10(abs(S) + eps);
imagesc(t_stft, f, stft_dB, 'Interpolation', 'bilinear');
axis xy;
colormap('jet');
ylim([0 800]);
caxis([-80 0]);
xlabel('Time (s)'); ylabel('Frequency (Hz)');
colorbar('vert');
  2 Comments
Abo
Abo on 22 Apr 2025
Thanks, Mathieu. I’ll check out the CWT method. Your input already helped improve the STFT plot. Much appreciated.
Mathieu NOE
Mathieu NOE on 22 Apr 2025
as always, my pleasure !

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!