annotate dsptools.mm @ 15:d5758530a039 tip

oF0.84 Retina, and iPhone support
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Tue, 12 May 2015 15:48:52 +0100
parents c667dfe12d47
children
rev   line source
rt300@0 1 //
rt300@0 2 // dsptools.cpp
rt300@0 3 // wablet
rt300@0 4 //
rt300@0 5 // Created by Robert Tubb on 21/06/2011.
rt300@0 6 // Copyright 2011 __MyCompanyName__. All rights reserved.
rt300@0 7 //
rt300@0 8
rt300@0 9 #include "dsptools.h"
rt300@0 10 #include "ofMain.h"
rt300@0 11
rt300@0 12 DSPTools::DSPTools(){
rt300@0 13 for(int i = 0; i < 3; i++){
rt300@0 14 xv[i] = 0.0;
rt300@0 15 yv[i] = 0.0;
rt300@0 16 }
rt300@0 17 getHPCoefficientsButterworth2Pole(ax, by);
rt300@0 18 }
rt300@0 19
rt300@0 20 double DSPTools::highpass1(double ax){
rt300@0 21 static double xm1 = 0.0;
rt300@0 22 double sample;
rt300@0 23 sample = 2*(ax - xm1);
rt300@0 24 xm1 = ax;
rt300@0 25 return sample;
rt300@0 26
rt300@0 27 }
rt300@0 28 double DSPTools::lowpass1(double ax){
rt300@0 29 static double xm1 = 0.0;
rt300@0 30 double sample;
rt300@0 31 sample = 0.5*(ax + xm1);
rt300@0 32 xm1 = ax;
rt300@0 33 return sample;
rt300@0 34
rt300@0 35 }
rt300@0 36
rt300@0 37 void DSPTools::getLPCoefficientsButterworth2Pole(const int samplerate, const double cutoff, double* const ax, double* const by)
rt300@0 38 {
rt300@0 39 double sqrt2 = 1.4142135623730950488;
rt300@0 40
rt300@0 41 double QcRaw = (2 * PI * cutoff) / samplerate; // Find cutoff frequency in [0..PI]
rt300@0 42 double QcWarp = tan(QcRaw); // Warp cutoff frequency
rt300@0 43
rt300@0 44 double gain = 1 / (1+sqrt2/QcWarp + 2/(QcWarp*QcWarp));
rt300@0 45 by[2] = (1 - sqrt2/QcWarp + 2/(QcWarp*QcWarp)) * gain;
rt300@0 46 by[1] = (2 - 2 * 2/(QcWarp*QcWarp)) * gain;
rt300@0 47 by[0] = 1;
rt300@0 48 ax[0] = 1 * gain;
rt300@0 49 ax[1] = 2 * gain;
rt300@0 50 ax[2] = 1 * gain;
rt300@0 51 }
rt300@0 52 void DSPTools::getHPCoefficientsButterworth2Pole(double* const ax, double* const by)
rt300@0 53 {
rt300@0 54 ax[0] = 0.997987115675119;
rt300@0 55 ax[1] = -1.995974231350238;
rt300@0 56 ax[2] = 0.997987115675119;
rt300@0 57
rt300@0 58 by[0] = 1.000000000000000;
rt300@0 59 by[1] = -1.995970179642828;
rt300@0 60 by[2] = 0.995978283057647;
rt300@0 61 }
rt300@0 62
rt300@0 63 double DSPTools::butter(double sample)
rt300@0 64 {
rt300@0 65
rt300@0 66 xv[2] = xv[1];
rt300@0 67 xv[1] = xv[0];
rt300@0 68 xv[0] = sample;
rt300@0 69 yv[2] = yv[1];
rt300@0 70 yv[1] = yv[0];
rt300@0 71
rt300@0 72 yv[0] = (ax[0] * xv[0] + ax[1] * xv[1] + ax[2] * xv[2]
rt300@0 73 - by[1] * yv[1]
rt300@0 74 - by[2] * yv[2]);
rt300@0 75
rt300@0 76 return yv[0];
rt300@0 77
rt300@0 78 }