Is there a way to filter out a frequency?

Question:

np.argmax function returns the highest frequency value and I want to take that value out and then see what the second highest value is.

import wave
import struct
import matplotlib.pyplot as plt
import numpy as np

There is some code here but that just defines the data of the sound wave and has nothing to do with my question.

data_fft = np.fft.fft(data)
frequencies = np.abs(data_fft)
print("The frequency is {} Hz".format(np.argmax(frequencies)))
frequency1 = frequencies - np.argmax(frequencies)
print(np.argmax(frequency1))

I can not figure out how to remove the highest frequency value. The code I showed returns 4561 as the highest value in frequencies and then I try to subtract that out in the new variable frequency1. When I print out the np.argmax of that I still get 4561. Does anyone know how I can remove this value?

Asked By: thiccdan69

||

Answers:

np.argmax() gives you the indices of the maximum values along some axis, see the documentation.

You subtract the indices from the values with:

frequency1 = frequencies - np.argmax(frequencies)

Instead, you wanted to remove the values at these indices, and the obtain the maximum value in the remaining data:

import numpy as np

data_fft = np.random.random(100)  # I don't have your data, so generating some
frequencies = np.abs(data_fft)
print("The highest frequency is at position {}".format(np.argmax(frequencies)))
new_frequencies = np.delete(frequencies, np.argmax(frequencies))
print("The 2nd highest frequency is at position {}".format(np.argmax(new_frequencies)))
print("And its value is {}".format(np.max(new_frequencies)))

Although it’s a matter of style, the default string delimiter for Python is a single quote ' and you may find f-strings more readable:

import numpy as np

data_fft = np.random.random(100)
frequencies = np.abs(data_fft)

print(f'The highest frequency is at position {np.argmax(frequencies)}')
new_frequencies = np.delete(frequencies, np.argmax(frequencies))

print(f'The 2nd highest frequency is at position {np.argmax(new_frequencies)}')
print(f'And its value is {np.max(new_frequencies)}')
Answered By: Grismar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.