在matlab中读取wav文件和FFT [英] Audioread in matlab of wav file and FFT

查看:68
本文介绍了在matlab中读取wav文件和FFT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Matlab,我想对之前在 Matlab 上录制的 wav 文件执行 FFT.

I'm working on Matlab, I want to perform FFT on a wav file I previously recorded on Matlab as well.

fs = 44100; % Hz
t = 0:1/fs:1; % seconds
f = 600; % Hz

y = sin(2.*pi.*f.*t);

audiowrite('600freq.wav',y,fs)

这是我在 wav 文件中录制的方式.现在进入阅读和 FFT 部分:

This is the way I'm recording in the wav file. Now to the reading and FFT part:

[y,Fs] = audioread('600freq.wav');
sound(y)
plot(fft(y))

这是我得到的 FFT 图:

This is the plot of the FFT I get:

也许我遗漏了一些关于 FFT 的内容,但我预计会有两个垂直棒棒糖.我注意到的另一件事是错误的,当我从文件中读取声音后播放声音时,它更长并且音高明显更低.我的猜测是采样率问题,但我真的不知道该怎么办.

Maybe I'm missing something about the FFT, but I expected two vertical lollipops. Another thing I noticed that's wrong, is when I play the sound after reading it form the file it's longer and the pitch is significantly lower. My guess is a sampling rate problem, but I really have no idea of what to do about it.

提前感谢您的帮助.

推荐答案

那是因为你没有绘制幅度.您绘制的是系数,但这些是复数值.因此,横轴是实部,纵轴是虚部.此外,当您使用 sound 时本身,默认采样频率为 8 kHz(准确地说是 8192 Hz),这解释了为什么您的声音具有较低的音高.您需要使用采样频率作为 sound 的第二个参数,这是由 audioread 的第二个输出提供的.

That's because you're not plotting the magnitude. What you are plotting are the coefficients, but these are complex valued. Because of that, the horizontal axis is the real component and the vertical axis is the imaginary component. Also, when you use sound by itself, the default sampling frequency is 8 kHz (8192 Hz to be exact) which explains why your sound is of a lower pitch. You need to use the sampling frequency as a second argument into sound, and that's given to you by the second output of audioread.

因此,尝试将 abs 放在 fft 调用之后,并在 sound 中使用 Fs:

So, try placing abs after the fft call and also use Fs into sound:

[y,Fs] = audioread('600freq.wav');
sound(y, Fs);
plot(abs(fft(y)))

此外,上面的代码没有正确绘制水平轴.如果您想这样做,请确保您fftshift 进行傅立叶变换后的光谱,然后正确标记您的轴.如果您想根据频率确定每个水平值是多少,Paul R 的这篇很棒的帖子可以解决问题:我如何获得FFT 中的每个值?

Also, the above code doesn't plot the horizontal axis properly. If you want to do that, make sure you fftshift your spectra after you take the Fourier transform, then label your axis properly. If you want to determine what each horizontal value is in terms of frequency, this awesome post by Paul R does the trick: How do I obtain the frequencies of each value in an FFT?

基本上,FFT 中的每个水平值都是这样的:

Basically, each horizontal value in your FFT is such that:

F = i * Fs / N

i 是 bin 编号,Fs 是采样频率,N 是您用于 FFT 的点数.F 是您正在查看的组件的解释频率.

i is the bin number, Fs is the sampling frequency and N is the number of points you're using for the FFT. F is the interpreted frequency of the component you're looking at.

默认情况下,fft 假定 N 是数组中的总点数.对于单边 FFT,i0, 1, 2,floor((N-1)/2) 由于到奈奎斯特采样定理.

By default, fft assumes that N is the total number of points in your array. For the one-sided FFT, i goes from 0, 1, 2, up to floor((N-1)/2) due to the Nyquist sampling theorem.

因为您在尝试编写的代码中实际所做的是显示频谱的两侧,这就是为什么将频谱居中以便 DC 频率位于中间而左侧为负值的原因光谱,右侧为正光谱.

Because what you're actually doing in the code you tried to write is displaying both sides of the spectrum, that's why it's nice to centre the spectrum so that the DC frequency is located in the middle and the left side is the negative spectra and the right side is the positive spectra.

我们可以在此处将其合并到您的代码中:

We can incorporate that into your code here:

[y,Fs] = audioread('600freq.wav');
sound(y, Fs);
F = fftshift(abs(fft(y)));
f = linspace(-Fs/2, Fs/2, numel(y)+1);
f(end) = [];    
plot(f, F);

现在横轴反映每个分量的正确频率,纵轴反映每个分量的幅度.

The horizontal axis now reflects the correct frequency of each component as well as the vertical axis reflecting the magnitude of each component.

通过运行生成 600 Hz 正弦音的音频生成代码,然后使用上面的代码绘制频谱,我得到了:

By running your audio generation code which generates a sine tone at 600 Hz, and then the above code to plot the spectra, I get this:

请注意,我在光谱的正侧插入了一个工具提示......大约是 600 Hz!

Note that I inserted a tool tip right at the positive side of the spectra... and it's about 600 Hz!

这篇关于在matlab中读取wav文件和FFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆