How to schedule a wav file using FFT?

NOTE. This is not a duplicate, I have special requirements, besides related issues.

To start, I want to build the spectrum of an audio file (.wav) in the same way as what arrogance does (it looks like: How to draw a frequency spectrum from a Fourier transform ).

So far I can read and write wav files. But my problem is that I donโ€™t know exactly what values โ€‹โ€‹I need to pass to the FFT function. By the way, I am using Exocortex for FFT in C #. The FFT function requires me to pass an array of complex numbers with the correct size (512, 1024, ... I suppose), an optional integer parameter for the length and direction forward (forward / backward).

Concrete questions:

  • The complex (class) from the Exocortex library has two meanings: Real and Imaginary. I have an array of samples, so should it be real and which should be imaginary?
  • I have a wav file, so the length should be taken by a variable. How to pass this to the FFT function? Do I have to choose a size (512/1024 / etc.), Divide all samples by size, and then transfer all of it to the FFT?
  • How do you know which frequencies should be indicated on the x axis?
  • How do I build FFT data? (I want the x axis to be frequency and the y axis to decibels)

If you donโ€™t understand what I mean, try using Audacity, import the audio file and then click Analyze> Plot Spectrum. These are the things that you want to recreate. Please answer my question in detail, because I really want to find out. I have only a little information about this. I'm just new to digital signal processing. Also, as much as possible, do not direct me to other FFT sites because they do not specifically answer my question.


EDIT:

I did some reading and found out how FFT audio data, but only by degree 2. So how do I do the same in an audio file with a length that does not have degrees 2? According to some, I need to use a "window". I also worked a bit on this and found out that it only processes part of the waveform that needs to be processed later. Remember above that I want the FFT audio file not to be part of it. So what should I do now? Please, help: (

+6
source share
1 answer

Signature

public static void FFT( float[] data, int length, FourierDirection direction ) 
  • You pass an array of complex numbers, represented as pairs. Since you only have real numbers (samples), you should place your samples in even locations in the array - data [0], data [2], data [4], etc. Odd locations must be 0, data [1] = data [3] = 0 ...
  • Length is the number of samples for which you want to calculate your FFT; it should be exactly half the length of the data array. You can use FFT with all of your WAV or parts of it, depending on what you want to see. Audacity will display the power spectrum of the selected part of the file, if you want to do the same, transfer the entire WAV or selected parts.
  • FFT will only show frequencies up to half the sampling frequency. Thus, you should have values โ€‹โ€‹from 0 to half the sampling rate. The number of values โ€‹โ€‹depends on the number of samples that you have (the number of samples affects the accuracy of the calculation).
  • Audacity displays the power spectrum. You must take each pair of complex numbers in the resulting array and calculate its ABS. ABS is defined as sqrt (r ^ 2 + i ^ 2). Each ABS value will correspond to one frequency.

Here is an example of working code:

 float[] data = new float[8]; data[0] = 1; data[2] = 1; data[4] = 1; data[6] = 1; Fourier.FFT(data, data.Length/2, FourierDirection.Forward); 

I give him 4 samples, anyway. Therefore, I expect to get something only at frequency 0. And indeed, after starting, I get

data [0] == 1, data [2] == 1, data [4] == 1, data [6] == 1

And others - 0.

If I want to use complex array overloading

 Complex[] data2 = new Complex[4]; data2[0] = new Complex(1,0); data2[1] = new Complex(1, 0); data2[2] = new Complex(1, 0); data2[3] = new Complex(1, 0); Fourier.FFT(data2,data2.Length,FourierDirection.Forward); 

Please note that here the second parameter is equal to the length of the array, since each element of the array is a complex number. I get the same result as before.

I think I missed a complicated overload before. I seem to be less error prone and more natural to use, as long as your data already falls in pairs.

+6
source

Source: https://habr.com/ru/post/951371/


All Articles