分析以下代码% 定义原始信号参数 fs = 1000; % 采样率(Hz) t = 0:1/fs:1; % 时间向量 x = sin(2*pi*6*t) + 0.25*sin(2*pi*80*t); % 原始信号 % 添加随机噪声 noise = 0.1*randn(size(t)); % 随机噪声 x_noisy = x + noise; % 添加噪声后的信号 % 设计FIR滤波器 order = 50; % 滤波器阶数 fc = 10; % 截止频率(Hz) b = fir1(order, fc/(fs/2)); % FIR滤波器系数 % 应用FIR滤波器 y1 = filter(b, 1, x_noisy); % 滤波后的信号 y2 = filtfilt(b,1,x_noisy); % 绘制原始信号、添加噪声后的信号和滤波后的信号 figure; %plot(t, x); %hold on; plot(t, x_noisy); hold on; plot(t, y1,'bo'); hold on; plot(t,y2,'k--'); axis([0,0.4,-2,2]); legend("原始信号","filter","filtflit");
时间: 2024-04-03 13:32:11 浏览: 36
这段代码主要是对一个正弦信号添加随机噪声,然后设计并应用了一个FIR滤波器对信号进行滤波,最后绘制了原始信号、添加噪声后的信号和滤波后的信号的图像。下面是代码的具体分析:
1. 定义原始信号参数:定义采样率为1000Hz,时间向量为0到1秒,步长为1/fs。
2. 生成原始信号:使用sin函数生成一个6Hz和80Hz的正弦信号,并将它们相加得到原始信号x。
3. 添加随机噪声:生成一个标准差为0.1的随机噪声,并将其加到原始信号x上,得到添加噪声后的信号x_noisy。
4. 设计FIR滤波器:指定滤波器的阶数为50,截止频率为10Hz,使用fir1函数设计FIR滤波器,并得到滤波器系数b。
5. 应用FIR滤波器:使用filter函数对添加噪声后的信号x_noisy进行滤波,得到滤波后的信号y1。另外,使用filtfilt函数也对x_noisy进行滤波,得到滤波后的信号y2。
6. 绘制图像:使用plot函数绘制原始信号x、添加噪声后的信号x_noisy、滤波后的信号y1和y2的图像,并使用legend函数添加图例。axis函数用于设定图像的范围。
注:filtfilt函数是进行零相位滤波的函数,可以消除滤波器带来的相位延迟。
相关问题
分析此代码运行结果:% 定义采样率和截止频率 Fs = 1000; Fstop1 = 100; % 第一个截止频率 Fpass1 = 125; % 第一个通带频率 Fpass2 = 300; % 第二个通带频率 Fstop2 = 350; % 第二个截止频率 % 计算截止频率、通带频率和通带宽度 Wstop1 = Fstop1/(Fs/2); Wpass1 = Fpass1/(Fs/2); Wpass2 = Fpass2/(Fs/2); Wstop2 = Fstop2/(Fs/2); Apass = 1; % 通带最大衰减(dB) dev = [0.01 0.01]; % 通带和阻带最大波纹(dB) W = [Wstop1 Wpass1 Wpass2 Wstop2]; % 设计频带 % 使用fir1函数设计滤波器 b = fir1(30, W, 'bandpass', kaiser(31, 5)); % 输出滤波器的频率响应 freqz(b, 1, 1024, Fs); % 生成原始信号 t = 0:1/Fs:1; x = sin(2*pi*50*t) + sin(2*pi*200*t); % 滤波 y = filter(b, 1, x); % 绘制原始信号和滤波后的信号 subplot(2,1,1); plot(t, x); title('原始信号'); xlabel('时间(秒)'); ylabel('幅度'); subplot(2,1,2); plot(t, y); title('滤波后的信号'); xlabel('时间(秒)'); ylabel('幅度');
这段代码的作用是设计一个带通滤波器,将一个由两个正弦波组成的原始信号中的125Hz到300Hz之间的频率成分滤出来,并绘制出原始信号和滤波后的信号。
具体分析如下:
1. 设计带通滤波器
首先,定义了采样率和四个频率参数:第一个截止频率Fstop1、第一个通带频率Fpass1、第二个通带频率Fpass2和第二个截止频率Fstop2。接着,通过将这些频率归一化到采样率的一半(即0到1之间),计算出了四个归一化频率Wstop1、Wpass1、Wpass2和Wstop2,以及通带最大衰减Apass和最大波纹dev。最后,使用fir1函数设计了一个30阶的带通滤波器,采用Kaiser窗口。
2. 输出滤波器的频率响应
使用freqz函数输出滤波器的频率响应。在这个函数中,输入滤波器系数b和1作为分母,指定采样点数为1024,采样率为Fs,生成滤波器的频率响应图像。
3. 生成原始信号
使用采样率和时间向量生成了1秒钟的时间序列t。然后,设定两个正弦波的频率为50Hz和200Hz,并将它们相加,生成了一个由两个正弦波组成的原始信号x。
4. 滤波
使用filter函数对原始信号进行滤波,得到了滤波后的信号y。在这个函数中,将滤波器系数b和原始信号x输入,同时将1作为第二个参数(即滤波器的分母),表示使用直接I型结构进行滤波。
5. 绘制图像
最后,使用subplot函数将两个子图分别绘制在一个图像窗口中,分别显示原始信号和滤波后的信号。在绘制时,使用plot函数绘制出时间序列和幅度之间的关系,并使用title、xlabel和ylabel函数添加标题和坐标轴标签。
总之,该代码实现了一个带通滤波器,并使用该滤波器对一个由两个正弦波组成的原始信号进行了滤波。
用MATLAB实现连续时间信号的采样及重构 以f(t)=Sa(t)=(sin t)/t为例,分别以过采样、等采样、欠采样三种情况, 绘出原信号、采样信号、重构信号的时域波形图。
以下是一些可能的MATLAB代码,用于实现f(t) = Sa(t) = (sin t)/t 连续时间信号的采样和重构:
```matlab
% 定义采样率和采样时长
fs = 100; % 采样率
T = 2*pi; % 采样时长
ts = 1/fs; % 采样间隔
% 定义原始信号
t = -T/2:0.001:T/2;
x = sinc(t/T);
% 过采样,采样率为 400 Hz
fs1 = 400; % 过采样率
ts1 = 1/fs1; % 过采样间隔
n1 = ceil(T/ts1); % 过采样点数
t1 = 0:ts1:(n1-1)*ts1; % 过采样时间轴
x1 = sinc(t1/T); % 过采样信号
% 等采样,采样率为 100 Hz
t2 = -T/2:ts:T/2; % 等采样时间轴
x2 = sinc(t2/T); % 等采样信号
% 欠采样,采样率为 50 Hz
fs3 = 50; % 欠采样率
ts3 = 1/fs3; % 欠采样间隔
n3 = ceil(T/ts3); % 欠采样点数
t3 = 0:ts3:(n3-1)*ts3; % 欠采样时间轴
x3 = sinc(t3/T); % 欠采样信号
% 重构信号
x_r1 = sinc((t1 - n1*ts1/2)/T)*fs1*ts1; % 过采样信号重构
x_r2 = sinc((t2 - T/2)/T)*fs*ts; % 等采样信号重构
x_r3 = sinc((t3 - n3*ts3/2)/T)*fs3*ts3; % 欠采样信号重构
% 绘制图形
figure;
subplot(3,3,1); plot(t, x); title('Original Signal');
subplot(3,3,4); stem(t1, x1); title('Oversampled Signal, fs1 = 400 Hz');
subplot(3,3,7); plot(t, x_r1); title('Reconstructed Signal, fs1 = 400 Hz');
subplot(3,3,2); plot(t, x); title('Original Signal');
subplot(3,3,5); stem(t2, x2); title('Uniformly Sampled Signal, fs = 100 Hz');
subplot(3,3,8); plot(t, x_r2); title('Reconstructed Signal, fs = 100 Hz');
subplot(3,3,3); plot(t, x); title('Original Signal');
subplot(3,3,6); stem(t3, x3); title('Undersampled Signal, fs3 = 50 Hz');
subplot(3,3,9); plot(t, x_r3); title('Reconstructed Signal, fs3 = 50 Hz');
```
运行上述代码后,将会生成一个包含 9 个子图的图形窗口,其中每行对应一种采样情况,包括原始信号、采样信号以及重构信号的时域波形图。
相关推荐
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)