You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

clear; clc; close all;
% 1. 参数
fs = 10000; % 采样频率 10kHz
f0 = 1000; % 基频 1kHz
t_total = 1;
t = 0:1/fs:t_total-1/fs;
N = length(t);
% 2. 生成混合信号(强杂波+弱谐波+噪声)
r = 10*cos(2*pi*f0*t) + 0.5*cos(2*pi*2*f0*t); % 杂波10cos(2πf0t) 目标0.5cos(2π*2f0t)
r = r + 0.5*randn(size(r)); % 加上高斯噪声
% 3. 设计高通滤波器滤除1kHz保留2kHz
fc = 1500;
[n_butter, Wn] = buttord(fc/(fs/2), (fc+500)/(fs/2), 1, 40);
[b,a] = butter(n_butter, fc/(fs/2), 'high'); % 四阶巴特沃斯高通截止频率1500Hz
% 进行滤波
r_filtered = filtfilt(b,a,r);
% 画滤波器的幅频响应曲线
[h,f_req] = freqz(b,a,N,fs);
figure('Color','w');
plot(f_req, 20*log10(abs(h)));
xlabel('频率/Hz');
ylabel('幅度/dB');
title('高通滤波器幅频响应曲线');
xlim([0,5000]);
ylim([-100,5]);
% 4. 频谱计算
f = (0:N-1)*fs/N;
f = f(1:N/2+1);
R = abs(fft(r))/N; R = R(1:N/2+1);
R_filt = abs(fft(r_filtered))/N; R_filt = R_filt(1:N/2+1);
% 5. 绘图
figure('Color','w');
% 时域前后对比
subplot(2,1,1);
plot(t*1000, r, 'b'); hold on;
plot(t*1000, r_filtered, 'r','LineWidth',1.2);
xlim([0,5]);
xlabel('时间/ms');
ylabel('幅度');
legend('滤波前','滤波后');
title('滤波前后时域波形');
% 频域前后对比
subplot(2,1,2);
plot(f, 20*log10(R/max(R)), 'b'); hold on;
plot(f, 20*log10(R_filt/max(R_filt)), 'r');
xlim([0,5000]);
ylim([-100,10]);
xlabel('频率/Hz');
ylabel('幅度/dB');
legend('滤波前','滤波后');
title('滤波前后频谱');
% 6. 计算信干比 SIR
f1_idx = find(f>=1000,1); % 基频杂波是1kHz
f2_idx = find(f>=2000,1); % 二次谐波目标是2kHz
% 滤波前
P_signal_before = R(f2_idx)^2;
P_clutter_before = R(f1_idx)^2;
SIR_before = 10*log10(P_signal_before / P_clutter_before);
% 滤波后
P_signal_after = R_filt(f2_idx)^2;
P_clutter_after = R_filt(f1_idx)^2;
SIR_after = 10*log10(P_signal_after / P_clutter_after);
fprintf('滤波前 SIR = %.2f dB\n', SIR_before);
fprintf('滤波后 SIR = %.2f dB\n', SIR_after);
fprintf('SIR 提升 %.2f dB\n', SIR_after - SIR_before);