Skip to content

Commit

Permalink
Added a const array and a filter to show show that works.
Browse files Browse the repository at this point in the history
  • Loading branch information
berndporr committed Jan 18, 2019
1 parent 5e70b85 commit 690c54e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
9 changes: 4 additions & 5 deletions Fir1fixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/* (C) 2013 Graeme Hattan & Bernd Porr */
/* (C) 2013-2019 Graeme Hattan & Bernd Porr */

#include "Fir1fixed.h"

Expand Down Expand Up @@ -66,24 +66,23 @@ Fir1fixed::Fir1fixed(const char* coeffFile,
rewind(f);
}

assert (taps > 0);
if (taps < 1) throw std::invalid_argument("No data in the file.");

buffer = new short int[taps];
coefficients = new short int[taps];

assert( buffer != NULL );
assert( coefficients != NULL );

for(int i=0;i<taps;i++)
for(unsigned i=0;i<taps;i++)
{
float a;
// we scan first as float because C gets
// we scan first as float because C gets
// upset with numbers written as 6E2.
if (fscanf(f,"%f\n",&a)<1)
{
char tmp[256];
sprintf(tmp,"Could not read the coefficients from %s\n",coeffFile);
taps = 0;
throw std::invalid_argument(tmp);
}
coefficients[i] = (short int)(a);
Expand Down
4 changes: 2 additions & 2 deletions Fir1fixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ THE SOFTWARE.
class Fir1fixed {
public:
/**
* Coefficients as an array of short ints
* Coefficients as an array of short ints (and length provided separately)
* \param coefficients Array of the coefficients scaled up by 2^bitshift
* \param bitshift Scaling factor of the coefficients as power of 2: 2^bitshift
* \param number_of_taps Number of taps (= number of coefficients)
Expand All @@ -45,7 +45,7 @@ class Fir1fixed {
unsigned number_of_taps);

/**
* Coefficients as an array of const short ints (length automatically detected)
* Coefficients as an const array of short ints (and length automatically detected)
* \param _coefficients Array of the coefficients (const short) scaled up by 2^bitshift
* \param bitshift Scaling factor of the coefficients as power of 2: 2^bitshift
**/
Expand Down
12 changes: 8 additions & 4 deletions demo/fixeddemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ int main (int,char**)
// inits the filter
Fir1fixed fir("coeff12bit.dat",12);

// resets the delay line to zero
fir.reset ();

// gets the number of taps
int taps = fir.getTaps();

printf("taps = %d\n",taps);

const short int averageCoeff[] = {1,1,1,1};
Fir1fixed averageFilt(averageCoeff,2);

FILE *finput = fopen("ecg50hz.dat","rt");
FILE *foutput = fopen("ecg_filtered.dat","wt");
for(int i=0;i<10000;i++)
Expand All @@ -36,7 +36,11 @@ int main (int,char**)
// we scan as float because the %d gets confused
// with 6E2 which is outputted by octave
if (fscanf(finput,"%f\n",&a)<1) break;
short int b = fir.filter((short int)a);
short int b = (short int)a;
// 50Hz removal
b = fir.filter(b);
// smoothing
b = averageFilt.filter(b);
fprintf(foutput,"%f\n",(float)b);
}
fclose(finput);
Expand Down

0 comments on commit 690c54e

Please sign in to comment.