如何使用 MATLAB 中的 fft 从录制的声音中消除噪音?

发布于 2024-11-05 03:21:03 字数 674 浏览 8 评论 0原文

我想从录制的声音中消除噪音,并使其快速找到该声音的基频,但我不知道如何消除这些噪音。我正在录制物体从不同高度掉落的声音。我想找到录制声音的高度和最大频率之间的关系。

  [y,fs]=wavread('100cmfreefall.wav');

 ch1=y(:,1);
 time=(1/44100)*length(ch1);
t=linspace(0,time,length(ch1));


L=length(ch1);
 NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
Y1=log10(Y);
figure(1)

f = fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y1(1:NFFT/2+1))) ;

[b,a]=butter(10,3000/(44100/2),'high');
Y1=filtfilt(b,a,Y1);

% freqz(b,a)
figure(2)

plot(f,2*abs(Y1(1:NFFT/2+1))) ;

title('Single-Sided Amplitude Spectrum of y(t)');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|')
xlim([0 50000])


% soundsc(ch1(1:100000),44100)

I want to remove noises from a recorded sound and make the fft of it finding fundamental frequencies of that sound, but I don't know how to remove those noises. I'm recording the sound of falling objects from different heights. I want to find the relation between the height and the maximum frequency of the recorded sound.

  [y,fs]=wavread('100cmfreefall.wav');

 ch1=y(:,1);
 time=(1/44100)*length(ch1);
t=linspace(0,time,length(ch1));


L=length(ch1);
 NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
Y1=log10(Y);
figure(1)

f = fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y1(1:NFFT/2+1))) ;

[b,a]=butter(10,3000/(44100/2),'high');
Y1=filtfilt(b,a,Y1);

% freqz(b,a)
figure(2)

plot(f,2*abs(Y1(1:NFFT/2+1))) ;

title('Single-Sided Amplitude Spectrum of y(t)');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|')
xlim([0 50000])


% soundsc(ch1(1:100000),44100)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2024-11-12 03:21:03

说信号中有噪声是非常模糊的,根本不能传达太多信息。一些问题是:

  • 噪声是高频还是低频?
  • 它与信号频带分离得很好还是混合在一起?
  • 噪声是否遵循统计模型?可以将其描述为平稳过程吗?
  • 噪声是另一个确定性干扰信号吗?

您采取的方法当然取决于上述问题的答案。

然而,根据您描述的实验设置,我的猜测是您的噪声只是背景噪声,在大多数情况下,本质上可以近似为白色。 白噪声是指在所有频率下具有恒定功率的统计噪声模型。

最简单的方法是使用低通滤波器或带通滤波器仅保留您感兴趣的频率(如果您还不知道,快速查看频谱应该可以揭示这一点)。在我之前的回答中,关于使用过滤的相关问题MATLAB,我提供了创建低通滤波器和常见陷阱的示例。您也许可以通读一下,看看它是否对您有帮助。

一个简单的例子:

考虑一个频率为 50 Hz、采样频率为 1000 Hz 的正弦曲线。为此,我添加高斯白噪声,使 SNR 约为 -6dB。原始信号和噪声信号可以在下图的顶行中看到(仅显示了 50 个样本)。正如您所看到的,噪声信号看起来几乎没有希望,因为所有结构似乎都已被破坏。然而,进行 FFT,揭示了隐藏的正弦曲线(显示在底行中)

img src="https://i.sstatic.net/BGf6A.png" alt="在此处输入图像描述">

< 48 至 52 Hz 的窄带滤波器为我们提供了“干净”的信号。当然,由于噪声,幅度会有所损失。然而,信号已经从起初看似失败的原因中恢复。

在此处输入图像描述

如何进行取决于您的具体应用程序。但我希望这可以帮助您了解噪声过滤的一些基础知识。

编辑

@Shabnam:已经有近 50 条评论了,我真的没有看到你做出任何努力来理解或至少自己尝试。您确实应该学会阅读文档并学习概念并尝试它,而不是为每个错误都跑回来。无论如何,请尝试以下操作(根据您的代码进行修改)并在注释中显示输出。

[y,fs]=wavread('100cmfreefall.wav');
ch1=y(:,1);
time=(1/fs)*length(ch1);
t=linspace(0,time,length(ch1));
L=length(ch1);
NFFT = 2^nextpow2(L);
f = fs/2*linspace(0,1,NFFT/2+1);

[b,a]=butter(10,3e3/(fs/2),'high'); 
y1=filtfilt(b,a,ch1);

figure(1)
subplot(2,1,1)
Y=fft(ch1,NFFT)/L;
plot(f,log10(abs(Y(1:NFFT/2+1))))
title('unfiltered')

subplot(2,1,2)
Y1=fft(y1,NFFT)/L;
plot(f,log10(abs(Y1(1:NFFT/2+1))))
title('filtered')

Saying that there is noise in your signal is very vague and doesn't convey much information at all. Some of the questions are:

  • Is the noise high frequency or low frequency?
  • Is it well separated from your signal's frequency band or is it mixed in?
  • Does the noise follow a statistical model? Can it be described as a stationary process?
  • Is the noise another deterministic interfering signal?

The approach you take will certainly depend on the answers to the above questions.

However, from the experiment setup that you described, my guess is that your noise is just a background noise, that in most cases, can be approximated to be white in nature. White noise refers to a statistical noise model that has a constant power at all frequencies.

The simplest approach will be to use a low pass filter or a band pass filter to retain only those frequencies that you are interested in (a quick look at the frequency spectrum should reveal this, if you do not know it already). In a previous answer of mine, to a related question on filtering using MATLAB, I provide examples of creating low-pass filters and common pitfalls. You can probably read through that and see if it helps you.

A simple example:

Consider a sinusoid with a frequency of 50 Hz, sampled at 1000 Hz. To that, I add Gaussian white noise such that the SNR is ~ -6dB. The original signal and the noisy signal can be seen in the top row of the figure below (only 50 samples are shown). As you can see, it almost looks as if there is no hope with the noisy signal as all structure seems to have been destroyed. However, taking an FFT, reveals the buried sinusoid (shown in the bottom row)

enter image description here

Filtering the noisy signal with a narrow band filter from 48 to 52 Hz, gives us a "cleaned" signal. There will of course be some loss in amplitude due to the noise. However, the signal has been retrieved from what looked like a lost cause at first.

enter image description here

How you proceed depends on your exact application. But I hope this helped you understand some of the basics of noise filtering.

EDIT

@Shabnam: It's been nearly 50 comments, and I really do not see you making any effort to understand or at the very least, try things on your own. You really should learn to read the documentation and learn the concepts and try it instead of running back for every single error. Anyway, please try the following (modified from your code) and show the output in the comments.

[y,fs]=wavread('100cmfreefall.wav');
ch1=y(:,1);
time=(1/fs)*length(ch1);
t=linspace(0,time,length(ch1));
L=length(ch1);
NFFT = 2^nextpow2(L);
f = fs/2*linspace(0,1,NFFT/2+1);

[b,a]=butter(10,3e3/(fs/2),'high'); 
y1=filtfilt(b,a,ch1);

figure(1)
subplot(2,1,1)
Y=fft(ch1,NFFT)/L;
plot(f,log10(abs(Y(1:NFFT/2+1))))
title('unfiltered')

subplot(2,1,2)
Y1=fft(y1,NFFT)/L;
plot(f,log10(abs(Y1(1:NFFT/2+1))))
title('filtered')
葬シ愛 2024-11-12 03:21:03

你的问题的答案很大程度上取决于你所说的“噪声”的特征——它的频谱分布、噪声是否静止、噪声源(它是否源于环境或记录链?)。

如果噪声是平稳的,即其统计特性不随时间变化,您可以尝试仅记录几秒(10-15 是一个很好的初始猜测)噪声,执行 FFT,然后在 FFT 中减去噪声值bin n 来自测量 FFT bin n

您可以在此处阅读一些背景信息:http://en.wikipedia.org/wiki/Noise_reduction

Answer to your question is highly dependent on the characteristics of what you call "noise" - its spectral distribution, the noise being stationary or not, the source of the noise (does it originate in the environment or the recording chain?).

If the noise is stationary, i.e its statistical characteristics do not change over time, you can try recording a few seconds (10-15 is a good initial guess) of noise only, preform FFT, and then subtract the value of the noise in FFT bin n from your measurement FFT bin n.

You can read some background here: http://en.wikipedia.org/wiki/Noise_reduction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文