My Spectrogram doesn't look right, and has negative values

35 views (last 30 days)
Hi,
I am attempting to create a MATLAB App that displays a spectrogram for a chunk of audio. I have it displaying a spectrogram but I want it to look slightly different. The Spectrogram created by my application has negative values and is upside down for some reason. I have included an image of what my Spectrogram looks like and also a photo of what I want it to look like. I don't care so much about the colour scheme, I'm just unsure how to go about removing the negative values, flipping the Spectrogram so increased frequency is going up on the Y-axis, and how to get a non-linear scale for the Y-axis.
This is what my spectrogram looks like for a given piece of audio:
And this is what I would like it to look like using the same piece of audio:
Here is my code, where chunk is just a chunk of a larger piece of audio:
(The negative values in both yticks and yticklabels is just to illustrate that the negative values are there and displayed above positive ones)
[S, F, T] = stft(chunk, app.Fs,Window=kaiser(256,5),OverlapLength=220,FFTLength=512);
imagesc(app.Spectogram, T, F, rot90(log(abs(S'))));
axis(app.Spectogram, 'tight');
yticks(app.Spectogram, [-5000, 50, 100, 500, 1000, 2000, 5000, 10000]);
yticklabels(app.Spectogram, {'-5000Hz' ,'50Hz', '100Hz', '500Hz', '1000Hz', '2000Hz', '5000Hz', '10000Hz'});
set(gca,'YDir','normal');
set(gca, 'YScale', 'log');

Accepted Answer

Mathieu NOE
Mathieu NOE on 5 Dec 2022
hello
stft : use option 'FrequencyRange','onesided'
to have only positive frequencies (or use spectrogram)
also y log scale works fine with surf but not with imagesc
[signal, Fs] = audioread ("test_voice_mono.wav");
[S, F, T] = stft(signal,Fs,'Window',kaiser(256,5),'OverlapLength',220,'FFTLength',512,'FrequencyRange','onesided'); % or use spectrogram
surf(T,F,abs(S),'EdgeColor' ,'none');
view([0 90])
hcb = colorbar('vert');
set(get(hcb,'Title'),'String','dB')
axis('tight');
yticks([50, 100, 500, 1000, 2000, 5000, 10000]);
yticklabels({'50Hz', '100Hz', '500Hz', '1000Hz', '2000Hz', '5000Hz', '10000Hz'});
set(gca,'YDir','normal');
set(gca, 'YScale', 'log');

More Answers (0)

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!