Name

analyze()

Class

FFT

Description

Calculates the current frequency spectrum of the input signal. Returns an array with as many elements as this FFT analyzer's number of frequency bands. The frequency associated with each band of the spectrum is frequency = binIndex * sampleRate / (2*numBands).
The values of the resulting array show the amplitudes of pure tone components contained in the signal. If the signal is a sine with an amplitude of 1, the spectrum will have an absolute value of 1 (0 dB) at the frequency of the sine. For complex real-world signals the spectrum values will be much lower and usually don't exceed 0.05.

Examples

  • import processing.sound.*;
    
    FFT fft;
    AudioIn in;
    int bands = 512;
    float[] spectrum = new float[bands];
    
    void setup() {
      size(512, 360);
      background(255);
        
      // Create an Input stream which is routed into the Amplitude analyzer
      fft = new FFT(this, bands);
      in = new AudioIn(this, 0);
      
      // start the Audio Input
      in.start();
      
      // patch the AudioIn
      fft.input(in);
    }      
    
    void draw() { 
      background(255);
      fft.analyze(spectrum);
    
      for(int i = 0; i < bands; i++){
      // The result of the FFT is normalized
      // draw the line for frequency band i scaling it up by 5 to get more amplitude.
      line(i, height, i, height - spectrum[i]*height*5 );
      } 
    }
    

Syntax

  • fft.analyze()
  • fft.analyze(target)

Parameters

  • target(float[])if provided, writes the frequency spectrum into the given array. The array needs to have as many elements as this FFT analyzer's number of frequency bands.

Return

  • float[]