spectrogram-短时傅里叶变换

function[STFT_X,STFT_t,STFT_f]=myspectrogram(S,windows,nov,nfft,Fs)
tic,
%% S为输入语音片段,window为加的窗,nov为重叠点数,nff为采样点数fft,Fs为采样频率
L=length(S);
nsc=length(windows);%窗长
coln = fix((L-nov)/(nsc-nov));%分成片段数,列数
rown=floor(nfft/2)+1;  %行数

%%初始化变量
STFT_X0=zeros(nsc,coln); %分帧结果
index=1;%索引
%%分帧,加窗
index_L=nsc-nov; %窗移
for i=1:coln
    %分帧
    temp_S0=S(index:index+nsc-1,1);
    %加窗
    temp_S=temp_S0.*windows;
    %存到每一列
    STFT_X0(:,i)=temp_S(:,1);
    %将索引后移
    index=index+index_L;
end
STFT_X0=fft(STFT_X0,nfft,1);
%取一半
STFT_X= STFT_X0(1:rown,:);

%% 画频谱图
%%归一化
STFT_X1 = abs(STFT_X)/coln*2;

% convert amplitude spectrum to dB (min = -120 dB)
STFT_X1 = 20*log10(STFT_X1);  

%时间轴
nsc1=nsc/2;
nsc2=nsc-nov;
STFT_t=(nsc1:nsc2:nsc1+(coln-1)*nsc2)/Fs;
%频率轴
STFT_f=(0:rown-1)*Fs./nfft;

% plot the spectrogram
STFT_X1=STFT_X1';
figure
surf(STFT_f, STFT_t, STFT_X1);

shading interp
axis tight
box on
view(0, 90)
set(gca, 'FontName', 'Times New Roman', 'FontSize', 14)
xlabel('Frequency, Hz')
ylabel('Time, second')
title('Amplitude spectrogram of the signal')

handl = colorbar;
set(handl, 'FontName', 'Times New Roman', 'FontSize', 14)
ylabel(handl, 'Magnitude, dB')
toc

转载请注明出处:https://blog.csdn.net/qq_42263796/article/details/88558035

Guess you like

Origin blog.csdn.net/qq_42263796/article/details/88558035