Electronic – Using Pointers to implement a filter

cfilterpointers

I'm trying implement a fairy basic filter, and right now I have just some simple test code for me to enter in values. It errors out on the line when it gets to where I call the function I have written. I am fairly certain my error is coming from how I am using the pointers but I just don't understand pointers well enough to know what I'm doing wrong. Any thoughts on what I am doing wrong?

If it helps to understand what the filter should be doing: when a new value is input it is added to the to the buffer and the old state of what the average of the buffer was at is subtracted from the the buffer. the new average of the buffer is returned.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,a_buffer,b_buffer,valueToPrinta,valueToPrintb;
    printf("a value: ");
    scanf("%d",&a);
    printf("b value: ");
    scanf("%d",&b);

    valueToPrinta = integrate_filter_data_16bit(a, a_buffer, 2);
    valueToPrintb = integrate_filter_data_16bit(b, b_buffer, 2);

    printf("%d and %d", a_buffer,b_buffer);

    return 0;
}

int integrate_filter_data_16bit(int integrator_buffer_16bit, volatile int *pintegrator_accumulator16bit, int integrator_size_16bit)
{
  int temp_integrator_accumulator_16bit = *pintegrator_accumulator16bit;

  /* compute the new value of the IIR filter accumulator */
  *pintegrator_accumulator16bit = (int)integrator_buffer_16bit + (temp_integrator_accumulator_16bit - (temp_integrator_accumulator_16bit >> integrator_size_16bit));

  return((int)(*pintegrator_accumulator16bit >> integrator_size_16bit));
}

Best Answer

I think your problem is simple.

If your function accepts a pointer, you should use the & operator on the variable you pass to the function. In your case, the lines that say:

valueToPrinta = integrate_filter_data_16bit(a, a_buffer, 2);
valueToPrintb = integrate_filter_data_16bit(b, b_buffer, 2);

... should instead say...

valueToPrinta = integrate_filter_data_16bit(a, &a_buffer, 2);
valueToPrintb = integrate_filter_data_16bit(b, &b_buffer, 2);

Or alternatively you could change your variable declrations from:

int a,b,a_buffer,b_buffer,valueToPrinta,valueToPrintb;

... to:

int a,b,valueToPrinta,valueToPrintb;
int a_buffer[1],b_buffer[1];

... because then a_buffer and b_buffer, as symbols, are implicitly pointer variables equivalent to &(a_buffer[0]) and &(b_buffer[0]) respectively.

That being said, your printf's make no sense. If you put & before an expression, you get the address of the memory location allocated to the evaluation of that expression. If you don't put an & before an expression, you get the value stored at the memory location allocated to that expression. That's all there is to it.