窗函数与源信号行为分析

狭义的信号S(t)是以时间为自变量的一维函数,则定义域是无穷,只要时间存在信号便存在,而我们对信号的捕获天然是局部的,这种局部视为"窗函数"与源信号在时间的乘积,而窗函数作为非平稳性质的确定性能量信号,有诸多形式存在:矩形、三角形、汉明、汉宁...只要是个类似的能量函数都叫做窗函数,但最具代表性的窗函数往往具备在信号分析中具有特别优势,下面分别介绍两种:

关于上面的GIF图如下:

涉及MATLAB程序如下:

Matlab 复制代码
clc;clear;close all
fs = 10;    % 采样率
T = 1/fs;   % 采样周期
wT = 1;     % 窗口连续时间
wN = wT*fs; % 窗口离散点数
winN = 30;  % 动图窗长
f_range = linspace(-fs/2,fs/2,1e3);
A_arr = zeros(1,1e3);
f1_ridx = 1e3/2+1e2+1;
f2_ridx = 1e3/2+1.5e2+1;
f1_lidx = 1e3/2-1e2;
f2_lidx = 1e3/2-1.5e2;
f1 = fs/1e3*1e2;
f2 = fs/1e3*1.5e2;
disp(['频率1:',num2str(f1),'Hz']);
disp(['频率2:',num2str(f2),'Hz'])
A_arr([f1_lidx,f1_ridx]) = 1;
A_arr([f2_lidx,f2_ridx]) = 1.5;
figure
plot(f_range,A_arr,'LineWidth',1,'Color','blue');
xlim('tight')
ylim('padded')
xlabel('频率 Hz')
ylabel('幅度')
grid 
box
%% 矩形窗
% 连续
t = linspace(-wT/2,wT/2,1e4);
At = ones(1,length(t));
w = linspace(0,10*pi,1e4);
Aw = wT*abs(sin(0.5*wT*w(2:end))./(0.5*wT*w(2:end)));
Aw = [wT,Aw];
figure
subplot(121)
plot(t,At,'LineWidth',1,'Color','blue');
xlim('tight')
ylim('padded')
xlabel('time s')
ylabel('amplitude')
grid on
subplot(122)
plot(w,Aw/wT,'LineWidth',1,'Color','blue','HandleVisibility','off');
hold on
xline(2*pi/wT,'LineWidth',1,'LineStyle','-.','Color','red','DisplayName','单边谱主瓣零点');
xlim('tight')
xt = xticks;
xticks(sort([2*pi/wT, xt]));
xlabel('w rad/s')
ylabel('amplitude(normalize)')
grid on
legend
disp('矩形窗(连续):')
disp(['主瓣宽度:',num2str(4*pi/wT),'rad/s']);

% 离散
An = ones(1,wN);
w = linspace(0,pi,1e4);
Aw = abs(sin(0.5*w*wN)./sin(0.5*w));
Aw(1) = wN;
figure
subplot(121)
stem(0:wN - 1,An,'Color','blue','Marker','o','MarkerSize',5,'MarkerEdgeColor','red','MarkerFaceColor','white');
xlim('tight')
ylim('padded')
xlabel('samples')
ylabel('amplitude')
grid on
subplot(122)
plot(w,Aw/wN,'Color','blue','LineWidth',1,'HandleVisibility','off');
xline(2*pi/wN,'LineWidth',1,'LineStyle','-.','Color','red','DisplayName','单边主瓣零点');


w = (0:fs/wN:fs/2)*2*pi;
Aw = abs(sin(wN*T*w/2)./sin(T*w/2));
Aw(1) = wN;
hold on
stem(w/fs,Aw/wN,'LineWidth',1,'Color','blue','Marker','o','MarkerSize',5,'HandleVisibility','off', ...
    'MarkerEdgeColor','red','MarkerFaceColor','white');
xt = xticks;
xticks(sort([2*pi/wN, xt]));
xlim('tight')
xlabel('w rad')
ylabel('amplitude(normalize)');
grid on
legend
% 卷积
w = linspace(-pi,pi,1e3);
A_conved = sin(0.5*w*winN)./sin(0.5*w);

A_conved = A_conved / max(A_conved);
rect_conv = abs(conv(A_conved,A_arr));
conv_res = zeros(1,length(rect_conv));
conv_pro = [A_conved,zeros(1,length(A_arr) - 1)];

figure
subplot(211)
fig1 = plot(linspace(-fs,fs,length(conv_res)),conv_res,'LineWidth',1,'Color','blue');
xlim('tight')
ylim([-0.25,max(rect_conv)*1.5])
grid on
ylabel('Amplitude')
xlabel('Frequence Hz')
subplot(212)

plot(1:length(conv_pro),[zeros(1,length(A_conved) - 1),A_arr],'LineWidth',1,'Color','red');hold on
fig2 = plot(1:length(conv_pro),conv_pro,'LineWidth',1,'Color','blue');
xlim('tight');
ylim([-0.25,2]);
grid on
ylabel('Amplitude')
xlabel('Delay')
F1 = getframe(gcf);
I1 = frame2im(F1);
[I1,map1]=rgb2ind(I1,256);
imwrite(I1,map1,'gif1.gif','gif','Loopcount',inf,'DelayTime',0.005);
speed = 10;
for i = 2:length(rect_conv) / speed
    fig1.YData(1:i * speed) = rect_conv(1:i * speed);
    fig2.YData = [zeros(1,speed), fig2.YData(1:end - speed)];
    pause(0.1);
    F1 = getframe(gcf);
    I1 = frame2im(F1);
    [I1,map1]=rgb2ind(I1,256);
    imwrite(I1,map1,'gif1.gif','gif','WriteMode','append','DelayTime',0.005);
end
t = (0:winN - 1) / fs;
s = 1*cos(2*pi*f1*t) + 1.5*cos(2*pi*f2*t);
fft_res = fft(s);
fft_res = fftshift(abs(fft_res) / winN * 2);
figure 
stem(linspace(-fs/2,fs/2,length(fft_res)),fft_res,'LineWidth',1,'Color','blue','Marker','.','MarkerSize',5);
xlim('tight')
grid on
box on
xlabel('Frequence Hz')
ylabel('Amplitude')
figure
conv_res = rect_conv(length(A_conved) / 2:length(rect_conv) - length(A_conved) / 2);
plot(f_range,conv_res,'LineWidth',1,'Color','blue');
hold on
plot(f_range,A_arr,'LineWidth',1,'Color','red');
grid on
box on
xlim('tight');
xlabel('Frequence Hz')
ylabel('Amplitude')
%% 汉宁窗
% 连续
t = linspace(-wT/2,wT/2,1e4);
At = 0.5*(1+cos(2*pi*t/wT));
w = linspace(0,10*pi,1e4);
Aw = wT*abs(0.5*sin(w(2:end)*wT/2)./(w(2:end)*wT/2) + ...
    0.25*sin(w(2:end)*wT/2 - pi)./(w(2:end)*wT/2 - pi) + ...
    0.25*sin(w(2:end)*wT/2 + pi)./(w(2:end)*wT/2 + pi));
Aw = [wT / 2,Aw];
figure
subplot(121)
plot(t,At,'LineWidth',1,'Color','blue');
xlim('tight')
ylim('padded')
xlabel('time s')
ylabel('amplitude')
grid on
subplot(122)
plot(w,Aw/wT,'LineWidth',1,'Color','blue','HandleVisibility','off');
hold on
xline(4*pi/wT,'LineWidth',1,'LineStyle','-.','Color','red','DisplayName','单边谱主瓣零点');
xlim('tight')
xt = xticks;
xticks(sort([4*pi/wT, xt]));
xlabel('w rad/s')
ylabel('amplitude(normalize)')
grid on
legend
disp('汉宁窗(连续):')
disp(['主瓣宽度:',num2str(8*pi/wT),'rad/s']);

% 离散
n = 0:wN - 1;
An = 0.5*(1-cos(2*pi*n/(wN - 1)));
w = linspace(0,pi,1e4);
X = 2*pi/(wN - 1);

D0 = sin(wN*w/2) ./ sin(w/2);
D0(isnan(D0)) = wN;

Dp = sin(wN*(w-X)/2) ./ sin((w-X)/2);
Dp(isnan(Dp)) = wN;

Dm = sin(wN*(w+X)/2) ./ sin((w+X)/2);
Dm(isnan(Dm)) = wN;

Aw = abs(0.5*D0 + 0.25*Dp + 0.25*Dm);

figure
subplot(121)
stem(0:wN - 1,An,'Color','blue','Marker','o','MarkerSize',5,'MarkerEdgeColor','red','MarkerFaceColor','white');
xlim('tight')
ylim('padded')
xlabel('samples')
ylabel('amplitude')
grid on
subplot(122)
plot(w,Aw/wN,'LineWidth',1,'Color','blue','HandleVisibility','off'); grid on
xline(4*pi/(wN - 1)*wN/wN,'LineStyle','-.','Color','red','DisplayName','单边谱主瓣零点');
w = (0:fs/wN:fs/2)*2*pi;
X = 2*pi/(wN - 1);

D0 = sin(wN*w*T/2) ./ sin(w*T/2);
D0(isnan(D0)) = wN;

Dp = sin(wN*(w*T-X)/2) ./ sin((w*T-X)/2);
Dp(isnan(Dp)) = wN;

Dm = sin(wN*(w*T+X)/2) ./ sin((w*T+X)/2);
Dm(isnan(Dm)) = wN;

Aw = abs(0.5*D0 + 0.25*Dp + 0.25*Dm);
hold on
stem(w/fs,Aw/wN,'LineWidth',1,'Color','blue','Marker','o','MarkerSize',5,'HandleVisibility','off', ...
    'MarkerEdgeColor','red','MarkerFaceColor','white');
xlim('tight')
xt = xticks;
xticks(sort([4*pi/(wN - 1), xt]));
xlabel('w rad')
ylabel('amplitude(normalize)')
grid on
legend

% 卷积
w = linspace(-pi,pi,1e3);
X = 2*pi/(winN - 1);

D0 = sin(winN*w/2) ./ sin(w/2);
D0(isnan(D0)) = winN;

Dp = sin(winN*(w-X)/2) ./ sin((w-X)/2);
Dp(isnan(Dp)) = winN;

Dm = sin(winN*(w+X)/2) ./ sin((w+X)/2);
Dm(isnan(Dm)) = winN;

A_conved = 0.5*D0 + 0.25*Dp + 0.25*Dm;
A_conved = A_conved / max(A_conved);
hann_conv = abs(conv(A_conved,A_arr));
conv_res = zeros(1,length(hann_conv));
conv_pro = [A_conved,zeros(1,length(A_arr) - 1)];

figure
subplot(211)
fig1 = plot(linspace(-fs,fs,length(conv_res)),conv_res,'LineWidth',1,'Color','blue');
xlim('tight')
ylim([-0.25,max(hann_conv)*1.5])
grid on
ylabel('Amplitude')
xlabel('Frequence Hz')
subplot(212)

plot(1:length(conv_pro),[zeros(1,length(A_conved) - 1),A_arr],'LineWidth',1,'Color','red');hold on
fig2 = plot(1:length(conv_pro),conv_pro,'LineWidth',1,'Color','blue');
xlim('tight');
ylim([-0.25,2]);
grid on
ylabel('Amplitude')
xlabel('Delay')
F2 = getframe(gcf);
I2 = frame2im(F2);
[I2,map2]=rgb2ind(I2,256);
imwrite(I2,map2,'gif2.gif','gif','Loopcount',inf,'DelayTime',0.005);
speed = 10;
for i = 2:length(hann_conv) / speed
    fig1.YData(1:i * speed) = hann_conv(1:i * speed);
    fig2.YData = [zeros(1,speed), fig2.YData(1:end - speed)];
    pause(0.1);
    F2 = getframe(gcf);
    I2 = frame2im(F2);
    [I2,map2]=rgb2ind(I2,256);
    imwrite(I2,map2,'gif2.gif','gif','WriteMode','append','DelayTime',0.005);
end
t = (0:winN - 1) / fs;
n = 0:winN - 1;
win = 0.5*(1-cos(2*pi*n/(winN - 1)));
s = 1*cos(2*pi*f1*t) + 1.5*cos(2*pi*f2*t);
fft_res = fft(s .* win);
fft_res = fftshift(abs(fft_res) / winN * 4);
figure 
stem(linspace(-fs/2,fs/2,length(fft_res)),fft_res,'LineWidth',1,'Color','blue','Marker','.','MarkerSize',5);
xlim('tight')
grid on
box on
xlabel('Frequence Hz')
ylabel('Amplitude')
figure
conv_res = hann_conv(length(A_conved) / 2:length(hann_conv) - length(A_conved) / 2);
plot(f_range,conv_res,'LineWidth',1,'Color','blue');
hold on
plot(f_range,A_arr,'LineWidth',1,'Color','red');
grid on
box on
xlim('tight');
xlabel('Frequence Hz')
ylabel('Amplitude')
相关推荐
电磁脑机8 小时前
无总线场同步:意识本质、AGI困境与脑机革命的核心理论重构
分布式·神经网络·架构·信号处理·agi
YaraMemo2 天前
射频链的构成
5g·fpga开发·信息与通信·信号处理·射频工程
FPGA-ADDA2 天前
第一篇:软件无线电(SDR)概念与架构演进
fpga开发·信号处理·软件无线电·rfsoc·47dr
FPGA-ADDA2 天前
第二篇:RFSoC芯片架构详解——处理系统(PS)与可编程逻辑(PL)
嵌入式硬件·fpga开发·信号处理·fpga·47dr
运维儿2 天前
3.冲突域与广播域:二层网络的关键概念
网络协议·智能路由器·信号处理·linux 网络·云计算网络
T.i.s3 天前
番外(开源心电图数据库处理)--mit-bih-normal-sinus-rhythm-database的使用教程
matlab·信号处理·心电图
Pixlout4 天前
《7元算子理论白皮书 v0.98.1》
信息与通信·信号处理
达不溜的日记4 天前
AutoSAR通信概述-DBC文件
网络协议·信息与通信·信号处理
原来是猿4 天前
Linux 信号处理:Core vs Term 解析
信号处理
电磁脑机4 天前
和大脑正确交互的脑机接口研究推演理论
分布式·神经网络·架构·交互·信号处理