- clc;clear;close all;
- Fs = 1000; % Sampling frequency
- T = 1/Fs; % Sampling period
- L = 1500; % Length of signal
- t = (0:L-1)*T; % Time vector
-
- S = 0.7*sin(2*pi*50*t) + sin(2*pi*90*t);
-
- f = Fs*(0:(L/2))/L;
-
- Y = fft(S);
- P2 = abs(Y/L);
- P1 = P2(1:L/2+1);
-
- figure,plot(f,P1)
创建一个20Hz和50Hz的叠加波,使用并使用高通滤波器进行滤波后,在使用fft查看频谱,查看低频波是否被正常过滤,代码如下
- clc;clear;close all;
- Fs=1000;
- T=1/Fs;
- L=1500;
- t=(0:L-1)*T;
-
- X= sin(2*pi*t*20)+sin(2*pi*t*50);
-
- subplot(4,1,1);
- plot(t,X);
-
- f=(0:L/2)*Fs/L;
-
- Y=fft(X);
-
- P1=abs(Y/L);
- P2=P1(1:L/2+1);
- subplot(4,1,2);
- plot(f,P2);
- title('傅里叶结果');
- xlabel('频谱 Hz');
- ylabel('振幅');
-
- cutoff_freq=30;
- order=9;
- [b,a]=butter(order,cutoff_freq/(Fs/2),'high');
- filtered_signal=filter(b,a,X);
- subplot(4,1,3);
-
-
- plot(t,filtered_signal);
-
- % 计算滤波后的信号的傅里叶变换
- Y_filtered = fft(filtered_signal);
-
- % 计算振幅谱
- P1_filtered = abs(Y_filtered/L);
- P2_filtered = P1_filtered(1:L/2+1);
-
- % 计算频率向量
- f_filtered = (0:L/2)*Fs/L;
-
- % 绘制滤波后信号的频谱图
- subplot(4,1,4);
-
- plot(f_filtered, P2_filtered);
效果如图所示:
