How do I make python load a big(2hours) wave-file and convert it's contents into a time-frequency array?

StackOverflow https://stackoverflow.com/questions/8902820

  •  17-04-2021
  •  | 
  •  

Вопрос

I would like to access the array with something like array[5000][440] meaning 5000ms from the start and 440hz and it would give me a value of the frequency's amplitude at this very position.

I could not find something like that here, if there is, please point me to it.

Это было полезно?

Решение

You basically want a spectrogram. To get you started, go through your sound file in small chunks, where each chunk is, say, 1/10th of a second, and FFT each of these chunks. (Then, of course, to look up 5000ms and 440Hz, go to the FFT of the appropriate chunk.)

Другие советы

You're operating under a couple of misconceptions.

You can't get the frequency of a wave at a particular point in time. You need to select a window of time, including many points before and after the point of interest. The more points you include, the more resolution you'll have in your frequency breakdown. You'll need to run some sort of windowing function on those points, then subject them to a FFT.

Once you have the results of the FFT, the numbers will correspond to frequencies but it won't be a simple relationship. You don't have any control over the frequency corresponding to each output, that was already determined by the sampling frequency of your signal combined with the number of samples. I'm afraid I don't have the conversion formula at hand. Each frequency will have two components, a real and an imaginary, and the amplitude will be sqrt(r**2+i**2).

You can convert times and frequencies on fly. You have to use __getitem__ and probably lru_cache to store some values for further usage.

Let say that fourier is something like this

class Fourier():
   def __init__(self,a=10):
      self.a=a
   def __getitem__(self, index): 
      #this is function that calculates and returns value of my_furier
      return self.a+index

t=Fourier()
print(t[12.4])

You can apply same thing for accessing time from Fourier. So you can create new time object that enables you picking any valid time and returns that time or use some kind of interpolation to return values that are not in table.

If you will not be able to store all values in ram, you can use shelve module from standard library to store and acess items from disk and you can apply interface whit interpolation on it if required.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top