信号处理 #

一、信号生成 #

1.1 基本信号 #

matlab
% 参数设置
fs = 1000;              % 采样频率
t = 0:1/fs:1-1/fs;      % 时间向量

% 正弦波
f = 50;                 % 频率
sin_wave = sin(2*pi*f*t);

% 余弦波
cos_wave = cos(2*pi*f*t);

% 方波
square_wave = square(2*pi*f*t);

% 锯齿波
sawtooth_wave = sawtooth(2*pi*f*t);

% 脉冲信号
pulse = pulstran(t, [0.5], 'rectpuls', 0.1);

1.2 噪声信号 #

matlab
% 白噪声
noise = randn(size(t));

% 添加噪声
signal = sin(2*pi*50*t) + 0.5*randn(size(t));

1.3 扫频信号 #

matlab
% 线性扫频
chirp_signal = chirp(t, 0, 1, 500);  % 0到500Hz

% 对数扫频
chirp_log = chirp(t, 10, 1, 500, 'logarithmic');

二、频谱分析 #

2.1 FFT #

matlab
% 信号
fs = 1000;
t = 0:1/fs:1-1/fs;
signal = sin(2*pi*50*t) + 0.5*sin(2*pi*120*t);

% FFT
N = length(signal);
Y = fft(signal);
P2 = abs(Y/N);
P1 = P2(1:N/2+1);
P1(2:end-1) = 2*P1(2:end-1);

% 频率轴
f = fs*(0:(N/2))/N;

% 绘图
plot(f, P1);
xlabel('频率 (Hz)');
ylabel('幅值');
title('单边频谱');

2.2 功率谱密度 #

matlab
% 使用pwelch
[pxx, f] = pwelch(signal, [], [], [], fs);
plot(f, 10*log10(pxx));
xlabel('频率 (Hz)');
ylabel('功率谱密度 (dB/Hz)');

% 使用periodogram
[pxx, f] = periodogram(signal, [], [], fs);

2.3 短时傅里叶变换 #

matlab
% STFT
[s, f, t_stft] = spectrogram(signal, 256, 250, 256, fs);

% 绘制频谱图
imagesc(t_stft, f, abs(s));
axis xy;
xlabel('时间 (s)');
ylabel('频率 (Hz)');
colorbar;

三、滤波器设计 #

3.1 FIR滤波器 #

matlab
% 低通滤波器
fs = 1000;
fc = 100;              % 截止频率
order = 100;           % 滤波器阶数

b = fir1(order, fc/(fs/2));  % FIR低通

% 频率响应
freqz(b, 1, 1024, fs);

% 应用滤波器
filtered = filter(b, 1, signal);

3.2 IIR滤波器 #

matlab
% Butterworth滤波器
order = 6;
fc = 100;

[b, a] = butter(order, fc/(fs/2));  % 低通

% 带通滤波器
f_low = 50;
f_high = 150;
[b, a] = butter(order, [f_low, f_high]/(fs/2), 'bandpass');

% 应用滤波器
filtered = filtfilt(b, a, signal);  % 零相位滤波

3.3 滤波器类型 #

matlab
% Butterworth - 最平坦通带
[b, a] = butter(order, Wn);

% Chebyshev Type I - 通带波纹
[b, a] = cheby1(order, Rp, Wn);

% Chebyshev Type II - 阻带波纹
[b, a] = cheby2(order, Rs, Wn);

% Elliptic - 最陡峭过渡带
[b, a] = ellip(order, Rp, Rs, Wn);

四、滤波器可视化 #

4.1 频率响应 #

matlab
% 设计滤波器
[b, a] = butter(6, 0.2);

% 频率响应
[h, w] = freqz(b, a, 512);

% 绘图
figure;
subplot(2, 1, 1);
plot(w/pi, 20*log10(abs(h)));
xlabel('归一化频率');
ylabel('幅值 (dB)');
title('幅频响应');

subplot(2, 1, 2);
plot(w/pi, unwrap(angle(h)));
xlabel('归一化频率');
ylabel('相位 (rad)');
title('相频响应');

4.2 滤波器设计工具 #

matlab
% 打开滤波器设计工具
filterDesigner;

% 或使用命令行
designfilt('lowpassfir', 'FilterOrder', 100, 'CutoffFrequency', 100, ...
           'SampleRate', 1000);

五、实用示例 #

5.1 去除噪声 #

matlab
% 生成含噪信号
fs = 1000;
t = 0:1/fs:1;
clean = sin(2*pi*50*t);
noisy = clean + 0.5*randn(size(t));

% 设计低通滤波器
fc = 100;
[b, a] = butter(6, fc/(fs/2));

% 滤波
filtered = filtfilt(b, a, noisy);

% 绘图
figure;
subplot(3, 1, 1);
plot(t, clean);
title('原始信号');

subplot(3, 1, 2);
plot(t, noisy);
title('含噪信号');

subplot(3, 1, 3);
plot(t, filtered);
title('滤波后信号');

5.2 心电图信号处理 #

matlab
% 模拟ECG信号
fs = 360;
t = 0:1/fs:10;
ecg = 0.5*sin(2*pi*1*t) + sin(2*pi*5*t) + 0.3*randn(size(t));

% 带通滤波 (0.5-40 Hz)
[b, a] = butter(4, [0.5 40]/(fs/2), 'bandpass');
ecg_filtered = filtfilt(b, a, ecg);

% 去除基线漂移
[b, a] = butter(4, 0.5/(fs/2), 'highpass');
ecg_baseline = filtfilt(b, a, ecg_filtered);

六、总结 #

本章学习了:

  1. 信号生成:正弦波、方波、噪声
  2. 频谱分析:FFT、功率谱、STFT
  3. 滤波器设计:FIR、IIR、Butterworth
  4. 滤波器应用:去噪、信号处理

下一章将学习图像处理。

最后更新:2026-03-27