1
我一直在写一个非常简单的代码来去除信号中的噪声。信号只是一个正弦波,噪声是一个随机矩阵,噪声信号是两者的加法。MATLAB上的低通滤波器Butterworth滤波器
的代码是:
close all;clear;clc;
%% Declarations
ts = 0.001;
fs = 1/ts;
fc = 5;
t = 0:ts:2;
Wn = pi*fc/(2*fs);
n = 3;
%% Preparation
signal = cos(2*pi*fc*t);
noise = rand(1, length(signal)); % Generating Random Noise
noisySignal = signal + noise;
%% Filtering Stage
[b,a] = butter(n, Wn, 'low');
filteredSignal = filter(b, a, noisySignal);
filteredSignal = filteredSignal - mean(filteredSignal); % Subtracting the mean to block DC Component
%% Plotting
figure(1)
subplot(3,1,1)
plot(t, signal, 'linewidth', 1.5)
title('Signal')
ylim([-1.5 1.5])
grid minor
subplot(3,1,2)
plot(t, noise)
title('Noise')
ylim([-1.5 2])
grid minor
subplot(3,1,3)
plot(t, noisySignal)
title('Noisy Signal')
ylim([-1.5 1.5])
grid minor
figure(2)
plot(t, filteredSignal, 'r', 'linewidth', 1.5)
hold on
plot(t, signal, 'linewidth', 1.5)
hold off
legend('Filtered Signal', 'Original Signal')
grid minor
ylim([-1.5 1.5])
图2;这是比较滤波信号和原始信号的图形;总是显示如下图所示。
我相信Wn
变量是不正确的,但我不知道如何计算正确的归一化频率。
是不是该过滤信号有偏移吗? – Tes3awy
['rand()'](https://www.mathworks.com/help/matlab/ref/rand.html)添加的噪音在'(0,1)'范围内具有均匀分布,这平均输入偏移0.5。对于无偏噪声,您可能要使用'(2 * rand(...) - 1)'(范围为'(-1,1)')或'randn(...)'(高斯分配)。 – SleuthEye
非常感谢您的详细解释 – Tes3awy