A make work project to keep busy, a low pass digital filter. It us under the category of real time systems.

In a real implementation an analog to digital converter would be sampling a signal and pushing samples into a buffer the length of the number of filter coefficients. The filter has to execute and an output generated before the next input sample.

The filter is auto-regressive. At any sample n the filter operates on the sample plus the previous number of samples equal to the filter length.

There are different algorithms to generate filter coefficients. That would be a math thread.

The simulation is mot entirely accurate, it uses floating point. In a real system the output and input to analog digital converters are signed integers that limit the accuracy and resolution.

#include "steve.h""

#include <fstream>

#include <iomanip>

using namespace std;

void make_sin(float fs, double x[], double y[],int sf, double tmax, int msize) {

int i;

double dt,t;

dt = 1/fs;

t = 0;

for (i = 0; i < msize; i++) {

x* = t;*

y* = sin(_PI2 * sf * t);*

t += dt;

}

}

void fir_lp(double isig[], double osig[], double coef[], int lencoef, int lensig) {

// low pass filter

int i, j, k, n;

double acc;

double* buffer = new double[lencoef];

// initialize the input buffer

for (i = 0; i < lencoef; i++)buffer* = 0;*

k = 0;

for (i = 0; i < lensig; i++) {

acc = 0;

for (j = 0; j < lencoef; j++) acc += coef[j] * buffer[j]);

osig[k++] = acc; // output to digital to analog converter

//push new analog to digital converter sample onto the buffer.

for (n = 1; n < lencoef; n++)buffer[n - 1] = buffer[n];

buffer[lencoef - 1] = isig*;*

}

delete [] buffer;

}

int main()

{

double samp_freq,tmax;

int sig_freq, i, n, ncoef, msize;

// get filter coefficients

ifstream coeffile;

coeffile.open("coef.txt");

coeffile >> ncoef; // filter length

coeffile >> tmax; // simulation time

double* coef = new double[ncoef];

for (i = 0; i < ncoef; i++) coeffile >> coef*;*

coeffile.close();

sig_freq = 1; //hertz sampling frequency

samp_freq = 100; //hertz sample frequency

msize = ceil(tmax * samp_freq); //array size

double* x = new double[msize];

double* filtin = new double[msize];

double* filtout = new double[msize];

ofstream outfile;

outfile.open("lpout.txt");

make_sin(samp_freq, x, filtin, sig_freq,tmax,msize);

fir_lp(filtin, filtout, coef,ncoef,msize);

for(i=0;i < msize; i++)outfile << x* << " " << filtin** << " "<< filtout** << "\n"; *

outfile.close();

delete [] x;

delete [] filtin;

delete [] filtout;

return(0);

}