Python - 在ndarray中选择值范围

时间:2014-04-15 02:05:22

标签: python arrays multidimensional-array

我已使用wavfile.read将.wav文件导入Python并使用频率数据执行强度-vs-频率图(使用快速傅里叶变换)。

现在我试图隔离并绘制某些频率范围。我无法从array向量中提取索引值,以便识别相应的强度值。

我在频率向量上尝试了revelflatten,但仍然遇到'numpy.ndarray' object has no attribute 'index'错误。我的array实际上是ndarray吗?如果是这样,我该如何解决这个问题?

代码:

from __future__ import division
from scipy.io import wavfile
import numpy as np
import matplotlib.pyplot as plt

# enter directory of .wav file here:
directory = 'C:\'

# import .wav file
fs, samples = wavfile.read(str(directory)) # fs = sample rate
y = samples / np.linalg.norm(samples) # normalised sample values
t = np.arange(0,len(y))/fs # make time vector

plt.figure(0)
plt.plot(t,y)
plt.title('my waveform')
plt.xlabel('time (s)')
plt.ylabel('amplitude')

# power spectrum
powSpec = np.abs(np.fft.fft(y))**2
time_step = 1 / fs
freq = np.fft.fftfreq(len(y), time_step) # find frequency
idx = np.argsort(freq)
plt.figure(1)
f = freq[idx] # frequencies
ps = powSpec[idx] # power spectrum
plt.plot(f, ps)
plt.xlim(-2000, 2000)
plt.title('power spectrum')
plt.xlabel('frequency (Hz)')
plt.ylabel('power')
plt.show()

# remove freq components

def find_nearest(array, value):
    # function to extract frequency values and position in array
    idx = (np.abs(array-value)).argmin()
    return array[idx]

book = np.array([1,2,4,8,16,32,64]) # selecting orders of resonant frequencies
fnote = np.ndarray.ravel(f)

for n in book:
    f_mid = find_nearest(f, 523.25*n)
    f_low = find_nearest(f-25, 523.25*n-25)
    f_hi = find_nearest(f+25, 523.25*n+25)
    #print (fnote_low,fnote_mid,fnote_hi)
    print (fnote.index(f_mid))

在@desired登录建议时,我将print (fnote.index(f_mid))行更改为print (np.where(fnote==f_low),np.where(fnote==f_mid),np.where(fnote==f_hi)),然后我

((array([], dtype=int64),), (array([97065], dtype=int64),), (array([], dtype=int64),))
((array([], dtype=int64),), (array([99315], dtype=int64),), (array([], dtype=int64),))
((array([], dtype=int64),), (array([103815], dtype=int64),), (array([], dtype=int64),))
((array([], dtype=int64),), (array([112815], dtype=int64),), (array([], dtype=int64),))
((array([], dtype=int64),), (array([130815], dtype=int64),), (array([], dtype=int64),))
((array([], dtype=int64),), (array([166814], dtype=int64),), (array([], dtype=int64),))
((array([], dtype=int64),), (array([189629], dtype=int64),), (array([], dtype=int64),))
sample rate fs = 44100
Nyqvist frequency fn = 22050.0

为什么我会收到[]

0 个答案:

没有答案