如何在Python中提取与fft值相关的频率

发布于 2024-09-18 23:53:09 字数 51 浏览 4 评论 0 原文

我在 numpy 中使用了 fft 函数,这导致了一个复杂的数组。如何获得准确的频率值?

I used fft function in numpy which resulted in a complex array. How to get the exact frequency values?

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

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

发布评论

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

评论(3

客…行舟 2024-09-25 23:53:09

np.fft.fftfreq 告诉您与系数相关的频率:

import numpy as np

x = np.array([1,2,1,0,1,2,1,0])
w = np.fft.fft(x)
freqs = np.fft.fftfreq(len(x))

for coef,freq in zip(w,freqs):
    if coef:
        print('{c:>6} * exp(2 pi i t * {f})'.format(c=coef,f=freq))

# (8+0j) * exp(2 pi i t * 0.0)
#    -4j * exp(2 pi i t * 0.25)
#     4j * exp(2 pi i t * -0.25)

OP 询问如何找到以赫兹为单位的频率。
我相信公式是频率(Hz)=abs(fft_freq *帧速率)。

下面是一些代码来演示这一点。

首先,我们制作一个 440 Hz 的波形文件:

import math
import wave
import struct

if __name__ == '__main__':
    # http://stackoverflow.com/questions/3637350/how-to-write-stereo-wav-files-in-python
    # http://www.sonicspot.com/guide/wavefiles.html
    freq = 440.0
    data_size = 40000
    fname = "test.wav"
    frate = 11025.0
    amp = 64000.0
    nchannels = 1
    sampwidth = 2
    framerate = int(frate)
    nframes = data_size
    comptype = "NONE"
    compname = "not compressed"
    data = [math.sin(2 * math.pi * freq * (x / frate))
            for x in range(data_size)]
    wav_file = wave.open(fname, 'w')
    wav_file.setparams(
        (nchannels, sampwidth, framerate, nframes, comptype, compname))
    for v in data:
        wav_file.writeframes(struct.pack('h', int(v * amp / 2)))
    wav_file.close()

这将创建文件 test.wav
现在我们读入数据,对其进行 FFT,找到功率最大的系数,
并找到对应的fft频率,然后转换为赫兹:

import wave
import struct
import numpy as np

if __name__ == '__main__':
    data_size = 40000
    fname = "test.wav"
    frate = 11025.0
    wav_file = wave.open(fname, 'r')
    data = wav_file.readframes(data_size)
    wav_file.close()
    data = struct.unpack('{n}h'.format(n=data_size), data)
    data = np.array(data)

    w = np.fft.fft(data)
    freqs = np.fft.fftfreq(len(w))
    print(freqs.min(), freqs.max())
    # (-0.5, 0.499975)

    # Find the peak in the coefficients
    idx = np.argmax(np.abs(w))
    freq = freqs[idx]
    freq_in_hertz = abs(freq * frate)
    print(freq_in_hertz)
    # 439.8975

np.fft.fftfreq tells you the frequencies associated with the coefficients:

import numpy as np

x = np.array([1,2,1,0,1,2,1,0])
w = np.fft.fft(x)
freqs = np.fft.fftfreq(len(x))

for coef,freq in zip(w,freqs):
    if coef:
        print('{c:>6} * exp(2 pi i t * {f})'.format(c=coef,f=freq))

# (8+0j) * exp(2 pi i t * 0.0)
#    -4j * exp(2 pi i t * 0.25)
#     4j * exp(2 pi i t * -0.25)

The OP asks how to find the frequency in Hertz.
I believe the formula is frequency (Hz) = abs(fft_freq * frame_rate).

Here is some code that demonstrates that.

First, we make a wave file at 440 Hz:

import math
import wave
import struct

if __name__ == '__main__':
    # http://stackoverflow.com/questions/3637350/how-to-write-stereo-wav-files-in-python
    # http://www.sonicspot.com/guide/wavefiles.html
    freq = 440.0
    data_size = 40000
    fname = "test.wav"
    frate = 11025.0
    amp = 64000.0
    nchannels = 1
    sampwidth = 2
    framerate = int(frate)
    nframes = data_size
    comptype = "NONE"
    compname = "not compressed"
    data = [math.sin(2 * math.pi * freq * (x / frate))
            for x in range(data_size)]
    wav_file = wave.open(fname, 'w')
    wav_file.setparams(
        (nchannels, sampwidth, framerate, nframes, comptype, compname))
    for v in data:
        wav_file.writeframes(struct.pack('h', int(v * amp / 2)))
    wav_file.close()

This creates the file test.wav.
Now we read in the data, FFT it, find the coefficient with maximum power,
and find the corresponding fft frequency, and then convert to Hertz:

import wave
import struct
import numpy as np

if __name__ == '__main__':
    data_size = 40000
    fname = "test.wav"
    frate = 11025.0
    wav_file = wave.open(fname, 'r')
    data = wav_file.readframes(data_size)
    wav_file.close()
    data = struct.unpack('{n}h'.format(n=data_size), data)
    data = np.array(data)

    w = np.fft.fft(data)
    freqs = np.fft.fftfreq(len(w))
    print(freqs.min(), freqs.max())
    # (-0.5, 0.499975)

    # Find the peak in the coefficients
    idx = np.argmax(np.abs(w))
    freq = freqs[idx]
    freq_in_hertz = abs(freq * frate)
    print(freq_in_hertz)
    # 439.8975
相权↑美人 2024-09-25 23:53:09

这里我们处理 fft 的 Numpy 实现。

与 DFT 值相关的频率(在 Python 中)

通过 fft(快速傅里叶变换),我们了解了一个算法大家族的成员,这些算法能够快速计算 DFT、离散等采样信号的傅里叶变换。

DFT 转换 N 的有序序列 em> 复数转换为 N 个复数的有序序列,要理解的是这两个序列都是周期性且周期为 N

在许多情况下,您会想到

  • 在时域中定义的信号x,长度为N,以恒定间隔dt采样,
  • 其 DFT X(此处具体为 X = np.fft.fft(x)),其元素在频率轴上以采样率 dω 进行采样嗯>。

一些定义

  • 信号x的周期(又名持续时间²),在dt处采样,具有N个样本,是

    <前><代码> T = dt*N

  • 基频(以Hz为单位,以rad/s为单位)的X,你的DFT是

    <前><代码> df = 1/T
    dω = 2*pi/T # =df*2*pi

  • 最高频率是 奈奎斯特频率

    <前><代码> ny = dω*N/2

    (注意:奈奎斯特频率不是 dω*N)³

与 DFT 中特定元素相关的频率

对于给定索引 0 中的元素相对应的频率=n 可以按如下方式计算:

def rad_on_s(n, N, dω):
    return dω*n if n<N/2 else dω*(n-N)

或者在单次扫描中

ω = np.array([dω*n if n<N/2 else dω*(n-N) for n in range(N)])

,如果您更愿意考虑以 Hz 为单位的频率,s/ω/f/

f = np.array([df*n if n<N/2 else df*(n-N) for n in range(N)])

使用这些频率

如果您想修改原始信号x -> y 仅以频率函数的形式在频域中应用算子,方法是计算 ω

Y = X*f(ω)
y = ifft(Y)

引入 np.fft .fftfreq

当然,numpy 有一个方便的函数 np.fft.fftfreq,它返回无量纲频率,而不是有维频率 但这就像

f = np.fft.fftfreq(N)*N*df
ω = np.fft.fftfreq(N)*N*dω

因为 df = 1/TT = N/spssps 是每个样本的样本数) 一样简单第二)还可以写

f = np.fft.fftfreq(N)*sps

注释

  1. 与采样间隔dt对偶,有采样率,sr,或者在一个过程中采集了多少个样本时间单位;当然是dt=1/srsr=1/dt
  2. 说到持续时间,即使它相当常见,也隐藏了
    周期性的基本思想。
  3. 奈奎斯特频率的概念在任何涉及时间信号分析的教科书中以及链接的维基百科文章中都清楚地暴露出来。难道说信息不能被创造就足够了吗?

Here we deal with the Numpy implementation of the fft.

Frequencies associated with DFT values (in python)

By fft, Fast Fourier Transform, we understand a member of a large family of algorithms that enable the fast computation of the DFT, Discrete Fourier Transform, of an equisampled signal.

A DFT converts an ordered sequence of N complex numbers to an ordered sequence of N complex numbers, with the understanding that both sequences are periodic with period N.

In many cases, you think of

  • a signal x, defined in the time domain, of length N, sampled at a constant interval dt
  • its DFT X (here specifically X = np.fft.fft(x)), whose elements are sampled on the frequency axis with a sample rate .

Some definition

  • the period (aka duration²) of the signal x, sampled at dt with N samples is is

     T = dt*N
    
  • the fundamental frequencies (in Hz and in rad/s) of X, your DFT are

     df = 1/T
     dω = 2*pi/T # =df*2*pi
    
  • the top frequency is the Nyquist frequency

     ny = dω*N/2
    

    (NB: the Nyquist frequency is not dω*N

The frequencies associated with a particular element in the DFT

The frequencies corresponding to the elements in X = np.fft.fft(x) for a given index 0<=n<N can be computed as follows:

def rad_on_s(n, N, dω):
    return dω*n if n<N/2 else dω*(n-N)

or in a single sweep

ω = np.array([dω*n if n<N/2 else dω*(n-N) for n in range(N)])

if you prefer to consider frequencies in Hz, s/ω/f/

f = np.array([df*n if n<N/2 else df*(n-N) for n in range(N)])

Using those frequencies

If you want to modify the original signal x -> y applying an operator in the frequency domain in the form of a function of frequency only, the way to go is computing the ω's and

Y = X*f(ω)
y = ifft(Y)

Introducing np.fft.fftfreq

Of course numpy has a convenience function np.fft.fftfreq that returns dimensionless frequencies rather than dimensional ones but it's as easy as

f = np.fft.fftfreq(N)*N*df
ω = np.fft.fftfreq(N)*N*dω

Because df = 1/T and T = N/sps (sps being the number of samples per second) one can also write

f = np.fft.fftfreq(N)*sps

Notes

  1. Dual to the sampling interval dt there is the sampling rate, sr, or how many samples are taken during a unit of time; of course dt=1/sr and sr=1/dt.
  2. Speaking of a duration, even if it is rather common, hides the
    fundamental idea of periodicity.
  3. The concept of Nyquist frequency is clearly exposed in any textbook dealing with the analysis of time signals, and also in the linked Wikipedia article. Does it suffice to say that information cannot be created?
南笙 2024-09-25 23:53:09

频率只是数组的索引。在索引 n 处,频率为 2πn / 数组长度(每单位弧度)。考虑:

>>> numpy.fft.fft([1,2,1,0,1,2,1,0])
array([ 8.+0.j,  0.+0.j,  0.-4.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+4.j,
        0.+0.j])

结果在索引 0、2 和 6 处具有非零值。有 8 个元素。这意味着

       2πit/8 × 0       2πit/8 × 2       2πit/8 × 6
    8 e           - 4i e           + 4i e
y ~ ———————————————————————————————————————————————
                          8

The frequency is just the index of the array. At index n, the frequency is 2πn / the array's length (radians per unit). Consider:

>>> numpy.fft.fft([1,2,1,0,1,2,1,0])
array([ 8.+0.j,  0.+0.j,  0.-4.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+4.j,
        0.+0.j])

the result has nonzero values at indices 0, 2 and 6. There are 8 elements. This means

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