本文主要是介绍matlab中stft和spectrogram函数区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写在最开头,两个几乎没有差别。
结果一模一样,只是取了分帧加窗fft的不同点而已。
先贴上程序。
clear;
clc;
close all;[sig,fs] = audioread('1.wav');
window_len = 400;
nfft = 1024;
windowt = hamming(window_len);
hoplen = window_len/2;% matlab自带stft
[stfts,f_stft,t_stft] = stft(sig, fs, 'Window',windowt, 'OverlapLength',hoplen, 'FFTLength',nfft);
figure;
y_stft = abs(stfts(:,:));
imagesc(t_stft, f_stft, 20*log10(y_stft));
xlabel('Samples');
ylabel('Freqency');
title(['自带stft nfft=' num2str(nfft) ', windowlen=' num2str(window_len) ' 短时傅里叶变换']);
colorbar;% matlab自带spectrogram函数
[spectrograms, f_spectrograms, t_spectrograms, p] = spectrogram(sig, windowt, hoplen, nfft, fs);
figure;
y_spectrograms = abs(spectrograms(end:-1:1,:));
imagesc(t_spectrograms, f_spectrograms, 20*log10(y_spectrograms));
xlabel('Samples');
ylabel('Freqency');
xlabel('Samples');
ylabel('Freqency');
title(['自带spectrogram, nfft=' num2str(nfft) ', windowlen=' num2str(window_len)]);
colorbar;% N = 0; %选择一个帧验证,可注释掉
% sig_s = sig(window_len*N + 1 + hoplen : window_len*N + window_len + hoplen).*windowt;
% y_fft = fft(sig_s, nfft);
% f_fft = fs*(1:(nfft))/nfft;
% y_fft = abs(y_fft);
% figure;
% plot(f_fft,y_fft)% 自己写的fft加窗求得stft
frame_num = floor((length(sig)-window_len) / hoplen + 1);
my_spectrogram = zeros(nfft/2+1, frame_num);
my_stft = zeros(nfft, frame_num);
for i = 1:frame_numsig_sfft = sig(1+hoplen*(i-1) : hoplen*(i-1)+window_len).*windowt;yfft_new = abs(fft(sig_sfft, nfft));my_spectrogram(:,i) = [yfft_new(nfft/2+1 : end); yfft_new(1)];my_stft(:,i) = [yfft_new(nfft/2+2 : end); yfft_new(1 : nfft/2+1)];
end
figure;
imagesc(t_stft, f_stft, 20*log10(my_stft));
xlabel('Samples');
ylabel('Freqency');
title(['分帧加窗fft后的stft, nfft=' num2str(nfft) ', windowlen=' num2str(window_len)]);
colorbar;
if (length(unique(y_stft == my_stft)) == 1) % ans = 1 说明完全一致disp('stft 验证正确');
end
if (length(unique(y_spectrograms == my_spectrogram)) == 1)disp('spectrogram 验证正确');
end
输出
stft 验证正确
spectrogram 验证正确
点数对应关系:
这篇关于matlab中stft和spectrogram函数区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!