c# - How do I plot the spectrum of a wav file using FFT? -


note: not duplicate, have specific requirements other related questions.

to start with, want plot spectrum of audio file (.wav) audacity (similar: how draw frequency spectrum fourier transform).

so far able read , write wav files. problem don't know values need pass fft function. way using exocortex fft in c#. fft function requires me pass array of complex numbers right size (512, 1024, ... presume), optional integer parameter length, , fourier direction (forward/backward).

specific questions:

  1. the complex (class) exocortex library has 2 values namely real , imaginary. have array of samples, should real , should imaginary?
  2. i have wav file, length should assumed variable. how pass fft function? should select size (512/1024/etc), divide the entire samples size, pass of fft?
  3. how know frequencies should listed down on x-axis?
  4. how plot fft'ed data? (i want x-axis frequency, , y-axis in decibels)

if don't mean, try use audacity, import audio file, click analyze > plot spectrum. things want recreate. please answer question in details because want learn this. have little background on this. newbie in digital signal processing. as possible please don't direct me other fft sites because don't answer question specifically.


edit:

i've done reading , found out how fft audio data in powers of 2. how do same in audio file length that's not of powers of 2? according need use "window". i've done searching , found out takes portion of waveform processed later. remember above want fft of audio file not portion of it. should now? please :(

the signature is

public static void  fft( float[] data, int length, fourierdirection direction ) 
  1. you pass array of complex numbers, represented pairs. since have real numbers (the samples), should put samples in locations in array - data[0], data[2], data[4] , on. odd locations should 0, data[1] = data[3] = 0...
  2. the length amount of samples want calculate fft on, should half of length of data array. can fft entire wav or parts of - depends on wish see. audacity plot power spectrum of selected part of file, if wish same, pass entire wav or selected parts.
  3. fft show frequencies half of sampling rate. should have values between 0 , half sampling rate. amount of values depends on amount of samples have (the amount of samples affect precision of calculation)
  4. audacity plots power spectrum. should take each complex number pair in array receive , calculate abs. abs defined sqrt(r^2+i^2). each abs value correspond single frequency.

here's 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'm giving 4 samples, same. expect @ frequency 0. , indeed, after running it, get

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

and others 0.

if want use complex array overload

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 here second parameter equals length of array, since each array member complex number. same result before.

i think missed complex overload before. seems less error prone , more natural use, unless data comes in pairs.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -