diff tools/audio_processor_test/fft/checker/checker.cpp @ 23:90197e3375e2 pygar svn.24

[svn r24] added testing, but something is wrong with our c++ file.
author rlm
date Wed, 28 Apr 2010 08:19:09 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/audio_processor_test/fft/checker/checker.cpp	Wed Apr 28 08:19:09 2010 -0400
     1.3 @@ -0,0 +1,136 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <math.h>
     1.7 +#include <assert.h>
     1.8 +#include <string.h>
     1.9 +#include "SndfileWavUtil.h"
    1.10 +
    1.11 +const  int Taps = 9;
    1.12 +const  int Points = 8;
    1.13 +const  int log_Points = 3;
    1.14 +static int newest;
    1.15 +static short int x[Taps] = {0,0,0,0,0,0,0,0,0};
    1.16 +static short int fir(short int);
    1.17 +
    1.18 +// foreward declaration. This is defined in DFT.cpp
    1.19 +int DFT(int dir,int m,double *x1,double *y1);
    1.20 +
    1.21 +// lifted from the bluespec code
    1.22 +static double h[Taps] =  {-0.0124,
    1.23 +			  0.0,
    1.24 +			  -0.0133,
    1.25 +			  0.0,
    1.26 +			  0.8181,
    1.27 +			  0.0,
    1.28 +			  -0.0133,
    1.29 +			  0.0,
    1.30 +			  -0.0124};
    1.31 +
    1.32 +int
    1.33 +main (int argc, char * argv [])
    1.34 +{
    1.35 +  const char* inputWavFileName;
    1.36 +  const char* outputWavFileName;
    1.37 +
    1.38 +  FILE *inputPcmFile;
    1.39 +  FILE *firPcmFile;
    1.40 +  FILE *outputPcmFile;
    1.41 +
    1.42 +  short int sample;
    1.43 +  short int samples[Points];
    1.44 +
    1.45 +  inputWavFileName = argv[1];
    1.46 +  outputWavFileName = argv[2];
    1.47 +
    1.48 +  // Convert input wav to pcm
    1.49 +  generate_pcm(inputWavFileName, "input.pcm");
    1.50 +
    1.51 +  inputPcmFile  = fopen("input.pcm", "r");
    1.52 +  firPcmFile    = fopen("fir.pcm", "w");
    1.53 +
    1.54 +  assert(inputPcmFile);
    1.55 +  assert(firPcmFile);
    1.56 +
    1.57 +  newest = 0;
    1.58 +  memset(x, 0, sizeof(x));
    1.59 +
    1.60 +  while(fread(&sample, 2, 1, inputPcmFile)) {
    1.61 +    sample = fir(sample);
    1.62 +    assert(fwrite(&sample,2,1,firPcmFile));
    1.63 +  }
    1.64 +
    1.65 +  fclose(inputPcmFile);
    1.66 +  fclose(firPcmFile);
    1.67 +
    1.68 +  firPcmFile    = fopen("fir.pcm", "r");
    1.69 +  outputPcmFile = fopen("output.pcm", "w");
    1.70 +
    1.71 +  assert(firPcmFile);
    1.72 +  assert(outputPcmFile);
    1.73 +
    1.74 +  // we will do an Points point fft, and then undo it
    1.75 +  while(true) {
    1.76 +
    1.77 +    int read = fread(samples, 2,Points,firPcmFile);
    1.78 +
    1.79 +    if(read == 0)
    1.80 +      break;
    1.81 +
    1.82 +    // pad out the 
    1.83 +    if(read<Points){
    1.84 +      for(int i = read; i < Points; i++)
    1.85 +	samples[i] = 0;
    1.86 +    }
    1.87 +
    1.88 +    double dsamples[Points];
    1.89 +    double dimag[Points];
    1.90 +
    1.91 +    // This shift is performed in the HW
    1.92 +    for(int i = 0; i < Points; i++){
    1.93 +      dsamples[i] = (double)(samples[i]>>log_Points);
    1.94 +      dimag[i] = 0.0;
    1.95 +    }
    1.96 +
    1.97 +    DFT(1,Points,dsamples,dimag);
    1.98 +    DFT(-1,Points,dsamples,dimag);
    1.99 +
   1.100 +    for(int i = 0; i < Points; i++){
   1.101 +      samples[i] = (int)dsamples[i];
   1.102 +    }
   1.103 +
   1.104 +    if(read < Points){
   1.105 +      assert(fwrite(samples,2,read,outputPcmFile)==read);
   1.106 +      break;
   1.107 +    } else {
   1.108 +      assert(fwrite(samples,2,Points,outputPcmFile)==Points);
   1.109 +    }
   1.110 +  }
   1.111 +
   1.112 +  fclose(firPcmFile);
   1.113 +  fclose(outputPcmFile);
   1.114 +
   1.115 +  generate_wav("output.pcm", inputWavFileName, outputWavFileName);
   1.116 +
   1.117 +}
   1.118 +
   1.119 +short int
   1.120 +fir(short int sample)
   1.121 +{
   1.122 +
   1.123 +  x[newest] = sample; 
   1.124 +  double y = 0;
   1.125 +
   1.126 +  for (int k = 0; k < Taps; k++) { 
   1.127 +
   1.128 +    short int bits = x[(newest+k)%Taps];
   1.129 +    double x = (double)bits;
   1.130 +    y += h[k] * x; 
   1.131 +
   1.132 +  }
   1.133 +
   1.134 +  newest = (newest == 0) ? (Taps-1) : (newest-1);
   1.135 +
   1.136 +  short int rv = ((short int)y)&0x0000FFFF;
   1.137 +  return rv;
   1.138 +}
   1.139 +