我编写了这段代码是为了消除音频文件中的正弦波。
Fc = 40000; %Sampling rate
F0 = 400; %Notch frequency
Fs = Fc/2; %Nyquist frequency
Fn = F0/Fs; %Normalized frequency
r = 0.95;
num = [1 -2*cos(2*pi*Fn) 1]; % filter coefficients
den = [1 -2*r*cos(2*pi*Fn) r^2]; % filter coefficients
%Load original audio file
samples = [1, 5*Fc];
[clean_wav, Fc]=audioread('mustang.wav', samples);
originale_wav(:,2) = [];
%Add sine wave disturb
j = 1;
while j<(samples(2)+1),
t(j) = j/Fc;
j = j+1;
end;
x=sin(2*pi*F0*t);
disturbed_wav = clean_wav' + x;
filtered_wav = filter(num,den, x);
soundsc(filtered_wav, Fc);
过滤器剂量源完全消除了正弦波。我尝试过陷波滤波器的其他实现,但无论如何它都无法正常工作。 您能帮我找出错误的地方吗? 感谢您提供所有答案。
答案 0 :(得分:1)
看下面的例子。在过滤前后,您可以使用fft
查看信号的频率内容。然后,您可以清楚地看到信号发生了什么,正在过滤的频率。
% load a default sound
load handel.mat; % puts y and Fs in workspace
sound(y,Fs)
% Sample frequency and frequency to filter
%Fs = 8192; % from load handle.mat
F0 = 400; % frequency to filter
% time vector
t = (0:numel(y)-1).'/Fs;
% frequency vector for fft
f = 0:1/t(end):Fs;
% fft signal
Y = fft(y);
% add some sine wave with frequency F0 and small amplitude.
y_noise = y + 0.1*sin(2*pi*F0*t);
% listen to the sound
sound(y_noise, Fs)
% look at difference frequency content y and y_noise
Yn = fft(y_noise);
figure(2); clf;
plot(f,abs(Y),f,abs(Yn))
% filter
Wn = F0./Fs*2;
Q = 35;
[b,a] = iirnotch(Wn,Wn/Q);
y_filt = filter(b,a,y_noise);
% look at frequency response notch filter
freqz(b,a,[],Fs)
% fft to show frequency content filtered signal
Y_filt = fft(y_filter);
figure(3); clf;
plot(f,abs(Y),f,abs(Y_filt))
为说明起见,这是我使用的信号的频率成分,显然是400 Hz处的尖峰:
过滤后,这个高峰显然消失了:
但是当仔细观察频率时,您会发现您还在过滤400 Hz左右的频率,使用iirnotch
时可以使用Q因子对其进行调谐(蓝色信号是原始声音的英尺,橙色是英尺的英尺。滤波后的信号)。