1

I need to find the position of n specific value in the array FFT in this case the value is max2 = 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)

    

I need it so i can identify the point where there is the highest amplitude in certain frequency.

If you're interested heres the wav file: https://sndup.net/39bc

1 Answers1

0

Your code is a bit complicated, but if I understand you correctly, what you are asking is how to find the location (along x-axis) of the maximum. I do not know how the fact that this array came from FFT is relevant.

If that is what you are asking, I'm sure that is answered already, for example, this is related: Get the position of the biggest item in a multi-dimensional numpy array

A short answer is to use np.argmax or np.argwhere. For example:

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)])

The output should be

max value: 50
max position 5.0

In the previous example, I could also look for the x-values that correspond to specific y-values. For example: where y is -22.01:

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

and the output is:

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

I am guessing that in your code this would be something like:

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

Note I used np.isclose, because sometimes the equality does not hold, due to floating point precision.

Javier Gonzalez
  • 399
  • 4
  • 10