Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

FIR Band Pass filtering in processing

$
0
0
Hi and thanks,

I am a DSP beginner, so this question will be reflective of that.

I am trying to work my way through the implementation of band pass filtering with convolution.

My question is this. 

I have gone through the process of using AudioPlayer to import a wav file, do FFT of this and use the sinc(PI*x/PI*x) function to create a filter fft.

with these I can point wise multiply the FFT of the input with the filter FFT and get a resultant FFT, this is then Inverse FFT'd and the resulting array should contained the filter data?

Is there a set way to take an Array of floats and make them into an Audio output. So rather than data it comes full circle back to outputting a time based buffer that minim can play back as a filtered version of the original?

I have definatly got the FFT drawing out, I am wary of my sinc() function as when i draw this it doesnt seem to be what I expected.

Any pointer would be appreciated, or even just some suggested Tutorial. I am at college and I have to implement the process, rather than use minim's own band pass. 

Here is my dodgy code.

import ddf.minim.analysis.*;
import ddf.minim.*;
import ddf.minim.AudioSnippet;

Minim minim;
AudioPlayer jingle;
FFT fftAudioLeft, fftAudioRight, filterfft, fftResultLeft;
float[] filterData, resultLeft, resultFilter, finalArray, filtered;
int mySize;
float sampRate;
AudioSnippet[] sample = new AudioSnippet[mySize];

void setup() { 
  minim = new Minim(this);
  jingle = minim.loadFile("nuecut.wav", 2048);
  jingle.loop();
  mySize = jingle.bufferSize();
  sampRate = jingle.sampleRate();
  fftAudioLeft = new FFT(mySize, sampRate);
  fftAudioRight = new FFT(mySize, sampRate);
  fftResultLeft = new FFT(mySize, sampRate);
  filterData = new float[mySize];
  resultLeft = new float[mySize];
  resultFilter = new float[mySize];
  filtered = new float[mySize];
  finalArray = new float[mySize];
  size(fftAudioLeft.specSize(), 200, P3D);
  textMode(SCREEN);
  for (int i = 0; i<mySize;i++) {

    filterData[i] = sinc(i);
  }
  filterfft =new FFT(mySize, mySize);
}

void draw()
{
  background(0);
  stroke(255);
  float[] left = jingle.left.toArray();
  float[] right = jingle.right.toArray();
  fftAudioLeft.forward(left);
  fftAudioRight.forward(right);
  filterfft.forward(filterData);

  for (int i = 0;i<mySize;i++) {

    resultLeft[i] = fftAudioLeft.getFreq(i);
    resultFilter[i] = filterfft.getFreq(i);

    finalArray[i] = resultLeft[i]*resultFilter[i];
  }
  fftResultLeft.inverse(finalArray);

  for ( int i = 0;i<mySize;i++) {

    filtered[i] = fftResultLeft.getFreq(i);
  }

  for (int i = 0; i < fftAudioLeft.specSize(); i++)
  {
    stroke(125, 125, 125, 100);
    line(i, height, i, height - fftAudioLeft.getBand(i)*4);
  }
  for ( int i = 0; i < left.length - 1; i++ )
  {
    float x1 = map(i, 0, jingle.bufferSize(), 0, width);
    float x2 = map(i+1, 0, jingle.bufferSize(), 0, width);
    // we multiply the values returned by get by 50 so we can see the waveform
    stroke(255, 0, 255, 60);
    line(x1, height/4 - left[i]*50, x2, height/4 - left[i+1]*50);
    line(x1, 3*height/4 - right[i]*50, x2, 3*height/4 - right[i+1]*50);
  }
}

float sinc(float x) { //Design the filter using the sinc(x) function.

  if (x == 0.f) {
    return 1.f;
  }

  return sin((PI*x)/PI*x);
}

void stop()
{
  jingle.close();
  minim.stop();

  super.stop();
}








Viewing all articles
Browse latest Browse all 1768

Trending Articles