I have some confusion about the commond of STFT
2 views (last 30 days)
Show older comments
I want to apply the commond of STFT to solve my problem,but I face two problems.Firstly,my date's frequencyRange is 100-2000,there is no commond to set it.In the help page of STFT in mathwork.com,frequencyRange only have three choices--'centered' |'twosided' | 'onesided'.Secondly,I need to use different date to make some figures by using STFT.Different date may cause every figure has different scale of colorbar,I want all my figures have same scale of colorbar.
0 Comments
Accepted Answer
Bhavik Patel
on 20 May 2021
It is my understanding that you want to plot STFT for custom frequency range. You would also like to apply the same scale for colorbar to visualize plots for different data.
You can use the output arguments of stft function to visualize custom frequency range. To apply the same scale of colorbar, set colormap limits using caxis function.
The following example illustrates one way to achieve these objectives:
fs = 10e3; % Sampling frequency
LowerFreq = 100; % Lower frequency
UpperFreq = 2000; % Upper frequency
FFTLen = 512; % FFT length
% Generate a signal for two seconds
t = 0:1/fs:2;
x = vco(sin(2*pi*t),[500 1400],fs);
% Comute STFT
[S,F,T] = stft(x,fs,'FFTLength',FFTLen,'FrequencyRange','onesided');
% Compute FFT bins for lower and higher frequencies
LowerFreqIdx = ceil(LowerFreq*FFTLen/fs);
UpperFreqIdx = ceil(UpperFreq*FFTLen/fs)+1;
customFreqRange = LowerFreqIdx:UpperFreqIdx;
% Plot STFT
smag = mag2db(abs(S));
pcolor(T,F(customFreqRange),smag(customFreqRange,:))
xlabel('Time (s)')
ylabel('Frequency (Hz)')
shading flat
colorbar
caxis([-100 100]) % set colormap limits
Hope this helps!
More Answers (0)
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!