F
fdunne2
The following C-code implements a simple FIR filter:
//realtime filter demo
#include <stdio.h>
#include <stdlib.h>
//function defination
float rtFilter1(float *num, float *den, float *xPrev, float *yPrev);
void main()
{
float sig_in[30], numerator[3], denominator[3], x[3], y[2], s_out[30];
int i;
int kk;
//unit impulse
sig_in[0] = 1.0;
printf("unit impulse... \n");
printf("input element number: 1 = %f \n", sig_in[0]);
for(i = 1; i<30; i++)
{
sig_in = 0.0;
printf("input element number: %d = %f\n", i+1, sig_in);
}
//initialise previous input and output arrays
for(i = 0; i<3; i++)
{
x = 0.0;
}
for(i = 0; i<2; i++)
{
y = 0.0;
}
//set filter coefficients
numerator[0] = 1;
numerator[1] = 1.71;
numerator[2] = 0.94;
// test
kk = sizeof(numerator)/sizeof(float);
denominator[0] = 1;
denominator[1] = -1.865;
denominator[2] = 0.889;
printf("\n\noutput signal values...\n");
//print output signal
for(i = 0; i<30; i++)
{
x[0] = sig_in;
s_out = rtFilter1(numerator, denominator, x, y);
printf("output element number: %d = %f\n", i+1, s_out);
}
}
//a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)
// realtime filter - direct form I - single input(xPrev[0]) / single output(sig_out)
float rtFilter1(float *num, float *den, float *xPrev, float* yPrev)
{
//positive sum
float psum = 0.0;
//negitive sum
float nsum = 0.0;
float sig_out;
//get length of numerator coefficient array
int numLen = sizeof(num)/sizeof(float);
//get length of denominator coefficient array
int denLen = 3;
int i;
//calc. positive sum
for(i = 0; i<numLen; i++)
{
psum = psum + num*xPrev;
}
//calc. negitive sum
for(i = 1; i<denLen; i++) // i=1 in order to bypass first denominator array element, den[0] (=1)
{
nsum = nsum + den*yPrev[i-1];
}
//shift memory
//shift values in previous input array
for(i = 2; i<=numLen; i++) //the last element in xPrev is xPrev[numLen-1], ie length(xPrev) == length(num)
{
xPrev[(numLen-i)+1] = xPrev[numLen-i]; // shifts each array value up a element eg xPrev[1] = xPrev[0]
}
//shift values in previous output array
for(i = 3; i<=denLen; i++) // i=3 because yPrev has 1 less element than denLen
{
yPrev[(denLen-i)+1] = yPrev[denLen-i]; // shifts each aeeay value up a element eg yPrev[1] = yPrev[0]
}
//calc. output signal
yPrev[0] = psum - nsum;
sig_out = yPrev[0];
return sig_out;
} //end rtFilter1
My problem is as follows:
The following code calculates the number of elements in an array(num):
int numLen = sizeof(num)/sizeof(float);
This method of finding array length is used in rtFilter, however it produces the wrong result. It produces the correct result when used in the main function though.
i.e.
// test
kk = sizeof(numerator)/sizeof(float);
Does anyone know why I am getting two different results for basicly the same line of code?
Is there anyway I can find the length of the array(numerator) inside the function rtFilter()?
Note: I've used arrays of 4 elements here just to test the program. Usually the array lengths are unknown.
Thanks.
//realtime filter demo
#include <stdio.h>
#include <stdlib.h>
//function defination
float rtFilter1(float *num, float *den, float *xPrev, float *yPrev);
void main()
{
float sig_in[30], numerator[3], denominator[3], x[3], y[2], s_out[30];
int i;
int kk;
//unit impulse
sig_in[0] = 1.0;
printf("unit impulse... \n");
printf("input element number: 1 = %f \n", sig_in[0]);
for(i = 1; i<30; i++)
{
sig_in = 0.0;
printf("input element number: %d = %f\n", i+1, sig_in);
}
//initialise previous input and output arrays
for(i = 0; i<3; i++)
{
x = 0.0;
}
for(i = 0; i<2; i++)
{
y = 0.0;
}
//set filter coefficients
numerator[0] = 1;
numerator[1] = 1.71;
numerator[2] = 0.94;
// test
kk = sizeof(numerator)/sizeof(float);
denominator[0] = 1;
denominator[1] = -1.865;
denominator[2] = 0.889;
printf("\n\noutput signal values...\n");
//print output signal
for(i = 0; i<30; i++)
{
x[0] = sig_in;
s_out = rtFilter1(numerator, denominator, x, y);
printf("output element number: %d = %f\n", i+1, s_out);
}
}
//a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)
// realtime filter - direct form I - single input(xPrev[0]) / single output(sig_out)
float rtFilter1(float *num, float *den, float *xPrev, float* yPrev)
{
//positive sum
float psum = 0.0;
//negitive sum
float nsum = 0.0;
float sig_out;
//get length of numerator coefficient array
int numLen = sizeof(num)/sizeof(float);
//get length of denominator coefficient array
int denLen = 3;
int i;
//calc. positive sum
for(i = 0; i<numLen; i++)
{
psum = psum + num*xPrev;
}
//calc. negitive sum
for(i = 1; i<denLen; i++) // i=1 in order to bypass first denominator array element, den[0] (=1)
{
nsum = nsum + den*yPrev[i-1];
}
//shift memory
//shift values in previous input array
for(i = 2; i<=numLen; i++) //the last element in xPrev is xPrev[numLen-1], ie length(xPrev) == length(num)
{
xPrev[(numLen-i)+1] = xPrev[numLen-i]; // shifts each array value up a element eg xPrev[1] = xPrev[0]
}
//shift values in previous output array
for(i = 3; i<=denLen; i++) // i=3 because yPrev has 1 less element than denLen
{
yPrev[(denLen-i)+1] = yPrev[denLen-i]; // shifts each aeeay value up a element eg yPrev[1] = yPrev[0]
}
//calc. output signal
yPrev[0] = psum - nsum;
sig_out = yPrev[0];
return sig_out;
} //end rtFilter1
My problem is as follows:
The following code calculates the number of elements in an array(num):
int numLen = sizeof(num)/sizeof(float);
This method of finding array length is used in rtFilter, however it produces the wrong result. It produces the correct result when used in the main function though.
i.e.
// test
kk = sizeof(numerator)/sizeof(float);
Does anyone know why I am getting two different results for basicly the same line of code?
Is there anyway I can find the length of the array(numerator) inside the function rtFilter()?
Note: I've used arrays of 4 elements here just to test the program. Usually the array lengths are unknown.
Thanks.