短时傅里叶变换原理及其MATLAB实现(Short Time Fourier Transform,STFT)

短时傅里叶变换原理及其MATLAB实现(Short Time Fourier Transform,STFT)

1.短时Fourier变换原理(STFT原理)

信号x(t)短时Fourier变换定义为:
在这里插入图片描述
其中w(τ)为窗函数。

X(ω,t)中的时间t表示窗函数w(τ−t)的位置,随着窗函数在整个区间上的滑动,可获得信号x(τ)在 t 附近区域上对应的频谱。

信号短时Fourier变换是一种常用的信号时频分析方法。

2.DFT中的STFT原理

信号x(t)的STFT是一个积分运算,在实际计算中也可通过DFT来实现,即
在这里插入图片描述
其中:窗函数w[k]的宽度为N,x[k]为连续信号x(t)的抽样。

若抽样频率为fs,则存在 t=kT, T=1/fs。

3.时间分辨率和频谱分辨率
(2)时间分辨率
时间分辨率由时窗宽度Tp决定,公式为
Tp =NT=N/fsam
Tp越小,时间分辨率越高。

(2)频谱分辨率
频谱分辨率是指分辩信号中相邻谱峰的能力,公式为
在这里插入图片描述
∆fc越小,频谱分辨率越高。

4.不足
信号的STFT虽然能在一定程度上改善Fourier变换的不足,实现信号的时频分析,但其时间分辨率固定不变,因而不能有效地反映信号的突变程度,其应用受到局限

小波分析拓展了信号STFT,实现了一种新的时频分析方法,其时窗可以随着信号的频率增高而缩小,频率降低而增大,有效地解决信号短时Fourier变换的缺陷,因而得到广泛应用

5.Matlab代码实现

clear;
clc;

Fs=1e4;

t=0:1/Fs:4;
t1=0:1/Fs:1;

%10Hz的信号
x1=[sin(2*pi*10*t1)';zeros(Fs*3,1)]; 
%1000Hz的信号
x2=[zeros(Fs,1);sin(2*pi*1000*t1)';zeros(Fs*2,1)];
%2000Hz的信号
x3=[zeros(Fs*2,1);sin(2*pi*2000*t1)';zeros(Fs,1)];
%3000Hz的信号
x4=[zeros(Fs*3,1);sin(2*pi*3000*t1)'];
figure(1);
subplot(411);
plot(t,x1);
subplot(412);
plot(t,x2);
subplot(413);
plot(t,x3);
subplot(414);
plot(t,x4);

x=x1+x2+x3+x4;
figure(2);
subplot(211);
plot(t,x);
title('时域图');  
axis([0 4 -2 2]);

%FFT代码
X=fft(x);
N=length(x);
f=(1:N/2)*Fs/N;
subplot(212);
plot(f,abs(X(1:N/2)));
xlabel('f (Hz)');
ylabel('|X(f)|');
title('频谱图');  

%STFT关键代码
figure(3);
spectrogram(x,128,120,128,Fs,'yaxis');
title('时频图');  
function [S, f, t] = stft(x, fs, window, nfft, noverlap) % STFT - Short-time Fourier Transform % [S, f, t] = stft(x, fs, window, nfft, noverlap) % x: input signal % fs: sampling frequency % window: window function (default: hamming) % nfft: number of FFT points (default: length of window) % noverlap: number of samples overlapped between adjacent frames (default: 0) % S: STFT matrix (nfft/2+1 x nframes) % f: frequency vector % t: time vector % Example: % [x, fs] = audioread('speech.wav'); % [S, f, t] = stft(x, fs, hamming(512), 512, 256); % imagesc(t, f, 20*log10(abs(S))); % axis xy; colormap(jet); colorbar; xlabel('Time (s)'); ylabel('Frequency (Hz)'); % Written by Yuancheng Zhu (yzhu@nd.edu) % Last update: 2021/9/9 % Check inputs narginchk(2, 5); if nargin < 3 || isempty(window) window = hamming(256); end if nargin < 4 || isempty(nfft) nfft = length(window); end if nargin < 5 || isempty(noverlap) noverlap = 0; end if ~isvector(window) || ~isnumeric(window) error('Window function must be a numeric vector.'); end if ~isscalar(fs) || ~isnumeric(fs) || fs <= 0 error('Sampling frequency must be a positive scalar.'); end if ~isscalar(nfft) || ~isnumeric(nfft) || nfft <= 0 error('Number of FFT points must be a positive scalar.'); end if ~isscalar(noverlap) || ~isnumeric(noverlap) || noverlap < 0 error('Number of overlapped samples must be a non-negative scalar.'); end if noverlap >= length(window) error('Number of overlapped samples must be less than the window length.'); end % Calculate STFT parameters nframes = fix((length(x)-noverlap)/(length(window)-noverlap)); if nframes < 1 error('Signal is too short for STFT with the given parameters.'); end x = x(:); window = window(:); S = zeros(nfft/2+1, nframes); t = (nfft/2:nfft/2+nframes-1) / fs; % Compute STFT for i = 1:nframes idx = (1:length(window)) + (i-1)*(length(window)-noverlap); if idx(end) > length(x) idx = idx(idx <= length(x)); xw = [x(idx); zeros(length(window)-length(idx), 1)]; else xw = x(idx) .* window; end X = fft(xw, nfft); S(:, i) = X(1:nfft/2+1); end f = (0:nfft/2)' / nfft * fs; end
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值