【问题标题】:Find the position of a specific value in an array created by scipy.fftpack.in Python在 Python 中由 scipy.fftpack.in 创建的数组中查找特定值的位置
【发布时间】:2020-10-18 08:38:42
【问题描述】:

我需要在数组FFT中找到n个特定值的位置,本例中的值为ma​​x2 = 4403226.763754396

import scipy.io.wavfile as wavfile
import scipy
import scipy.fftpack as fftpk
import numpy as np
from matplotlib import pyplot as plt

# Reads VAV file
s_rate, signal = wavfile.read("A0.wav")


''' Plotting 211'''

# Plot signal in time domain
plt.subplot(211)
plt.plot(signal)
plt.xlabel('Time (Sec)')
plt.ylabel('Amplitude')


''' Printing Signals, Freq and FTT '''

print ('\n''Signal''\n')
print signal
len1 = len(signal)
print ('\n'+'Number of elements: ' + str(len1) + ' \n')
#print (' '.join(map(str, signal))) 

FFT = abs(scipy.fft(signal))
freqs = fftpk.fftfreq(len(FFT), (1.0/s_rate))

# Print not treated freq
print ('\n' + "Freq (not treated)" + '\n')
print (freqs)
len2 = len(freqs)
print ('\n'+'Number elements in freq: ' + str(len2) + ' \n')

#Print freq after FFT
print ('\n' + "FFT" + '\n')
print FFT
len3 = len(FFT)
print ('\n'+'Number elements in FTT: ' + str(len2) + ' \n')


''' Plotting 212 '''

# Plotting signal in Frequency Domain
plt.subplot(212)
freq0 = len(FFT)//2
freq1 = range(len(FFT)//2)
#Prints all freq (Hz)
#print freq1

plt.plot(freqs[range(len(FFT)//2)], FFT[range(len(FFT)//2)])                                                          
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')

''' Printing treated signal '''
print ('\n' + "Freq ( treated)" + '\n')
#print (freq1)
len2 = len(freq1)
print ('\n'+'Number elements in freq (tretaed): ' + str(len2) + ' \n')

''' List edit Signal '''

# Sorting the list 
freqs.sort()
max1 = max(freqs[range(len(FFT)//2)])
# Printing the largest element 
print('\n'+'Largest element of the signal is:' + str(max1) + '\n')

''' List edit FTT '''

# Sorting the list 
FFT.sort()
# Saving max element of FTT in var
max2 = max(FFT)
# Printing the largest element of Amplitude
print('Largest element of the amplitude is:' + str(max2) + '\n')


''' List edit Freq'''
freq1.sort()
max3 = max(freq1)
print('Largest element of the freq is:' + str(max3) + '\n')

# INSERT FUNCTION FOR ARRAY INDEX HERE

''' Plotting last graph '''

# Plotting graphs
plt.show()
#range(len(FFT)//2)

    

我需要它,以便我可以识别特定频率中振幅最高的点。

如果你有兴趣,这里是 wav 文件:https://sndup.net/39bc

【问题讨论】:

    标签: python arrays matplotlib fft wav


    【解决方案1】:

    您的代码有点复杂,但如果我理解正确,您要问的是如何找到最大值的位置(沿 x 轴)。我不知道这个数组来自 FFT 的事实有什么关系。

    如果这是您要问的,我确定已经回答了,例如,这是相关的:Get the position of the biggest item in a multi-dimensional numpy array

    简短的回答是使用 np.argmax 或 np.argwhere。例如:

    import numpy as np
    x = np.linspace(0, 10, 101)
    y = 2 - (x - 5)**2
    print('max value:', np.argmax(y))
    print('max position', x[np.argmax(y)])
    

    输出应该是

    max value: 50
    max position 5.0
    

    在前面的示例中,我还可以查找与特定 y 值对应的 x 值。例如:其中 y 为 -22.01:

    x[np.argwhere(np.isclose(y, -22.01))]
    

    输出是:

    array([[0.1],
           [9.9]])
    

    我猜在你的代码中这会是这样的:

    freqs[np.argmax(FFT)]
    # or
    freqs[np.argwhere(np.isclose(FFT, 4403226.763754396))]
    

    注意我使用了 np.isclose,因为有时由于浮点精度,等式不成立。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 2022-11-21
      • 2012-09-04
      • 2022-11-03
      相关资源
      最近更新 更多