如何计算DFT中每个仓的能量? [英] How to calculate the energy per bin in a DFT?

查看:301
本文介绍了如何计算DFT中每个仓的能量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试有关离散傅立叶变换的知识.

I am testing my knowledge about Discrete Fourier Transforms.

我现在正在测试的是如何使用DFT计算波的中心频率.

What I am testing now is how to calculate the central frequency of a wave using DFT.

为此,我使用以下代码创建正弦数据:

For that purpose I create a sinusoidal data using this code:

// create a 100 Hz wave with a sampling rate of 512 samples per second
var data : [Double] = []
for i in 0...511 {
  let t = Double(i) * 100/256
  let f = 10 * sin(2 * Double.pi * t)
  data.append(f)
}

然后我在data上进行DWT,得到两个向量,一个向量包含实部,另一个向量包含虚部.

Then I do a DWT on data and get two vectors, one containing the real part and one containing the imaginary part.

我知道在每个向量的内部都会有这个:

I understand that the inside each vector I will have this:

  1. 数据有512个样本
  2. 因此,从0到256的项将是正频率
  3. 以及从257到511的项,负频率
  4. 我可以舍弃负频率,并保持正频率在bin 0到255之间.
  5. 接脚0为直流电.我可以丢弃它.
  6. Bin 255将为256Hz,因为它是采样率的一半.

要查看我是否正确,我检查了256个纸箱并寻找最大的量值.在下面的公式中,具有最高幅度的bin将为K,我可以找到信号频率:

To see if I get it right, I check the 256 bins and look for the highest magnitude. The bin with the highest magnitude will be K on the following formula and I can find the signal frequency:

freq = (K + 1) * fps / N

K+1,因为我的第一个索引是0,并且我已经从数组中丢弃了DC,其中N是样本数.

K+1 because my first index is 0 and I have discarded DC from my array, and where N is the number of samples.

最大的问题是:如何计算每个仓的能量?

The big problem is: how do I calculate the energy per bin?

E[i] = sqrt(realPart[i] * realPart[i] + imaginaryPart[i] * imaginaryPart[i])

????

推荐答案

上面的轮廓着眼于点...以计算给定bin的大小

Your outline above looks on point ... to calculate the magnitude for a given bin

mag = 2.0 * math.Sqrt(real*real+imag*imag) / number_of_samples

其中number_of_samples是输入到fft调用中的数组的长度...执行fft的美丽之处在于,您可以然后对那组(freq,幅值,相移)应用傅立叶逆变换以返回源时域信号...这样做是验证您的过程是否正确的好方法

where number_of_samples is the length of array fed into the fft call ... beautiful aspect of doing a fft is you can then apply the inverse Fourier Transform on that set of ( freq, magnitude, phase shift) to return back your source time domain signal ... doing this is a nice way to validate your process is correct

傅立叶变换和傅立叶逆变换的魔术-示例:

您从一个浮点数组开始,该数组代表诸如音频,股市指数或任何时间序列之类的摆动……这是时域表示,因为它是曲线上的一组点,您可以将时间留给左手.右X轴和上,下Y轴是曲线的高度...然后将此数组输入到fft api调用中,它将在频域表示形式中返回给您相同的信息...其相同的信息只是以不同的表示形式...在freq域中,您将拥有一个数组,其中元素0始终为每秒0个周期的频率(DC偏移),然后在数组中进行迭代时,您可以使用公式来递增freq

you start with a floating point array which represents something which wobbles like audio, stock market index or any time-series ... this is the time domain representation since its a set of points on a curve where time is your left to right X axis and the up and down Y axis is the height of the curve ... then you feed this array into a fft api call which will return back to you the same information in its frequency domain representation ... its the same information just in a different representation ... in the freq domain you will have an array where element 0 is always frequency of 0 cycles per second (DC offset) then as you iterate across the array you increment freq using formula

incr_freq := sample_rate / number_of_samples

因此,在fft调用生成的复数数组中,每个元素都是给定频率的数据,其中每个元素只是一个复数...简单来说,该频率域表示只是一组频率,每个频率都体现了复数(A + Bi)可以用来计算频率的幅度和相移

so in the complex array generated by the fft call each element is the data for a given frequency where each element is simply a complex number ... in simple terms this freq domain representation is just a set of frequencies, each freq embodied by a complex number (A + Bi) which can be used to calc that freq's magnitude and phase shift

现在是有趣的部分...如果将此freq域数组发送到逆傅立叶变换,您将取回时域表示形式的原始数据

now is the interesting part ... if you send this freq domain array into an inverse Fourier Transform you will get back your original data which is in time domain representation

myAudio_TD(时域)->发送到fft-> myAudio_FD(频率域)

myAudio_TD ( time domain ) --> send to fft --> myAudio_FD ( freq domain )

然后您可以自由地按照相反的方式进行操作

then later you are free to do the reverse as in

myAudio_FD(频率域)->发送到反fft-> myAudio_TD(时域)

myAudio_FD ( freq domain ) --> send to inverse fft --> myAudio_TD ( time domain )

请注意,在此过程中,您先从数组myAudio_TD开始,然后将其发送到fft调用中,然后发送到反向fft调用中,以神奇的方式返回给您原始的myAudio_TD

notice in that progression you started with an array myAudio_TD which got sent into an fft call then into an inverse fft call which magically returns back to you your original myAudio_TD

要查看从fft调用返回的复杂数组的完整解析,其中包括奈奎斯特限制的概念,请参见

to see the full parsing of the complex array returned from fft call which includes notion of Nyquist Limit see Get frequency with highest amplitude from FFT

这篇关于如何计算DFT中每个仓的能量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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