Show C language implementation of continuous moving average filter in recursive form
- User
Show C language implementation of continuous moving average filter in recursive form
- Assistant
Show C language implementation of continuous moving average filter in recursive form
A continuous moving average filter can be implemented in a recursive form using the formula for calculating the moving average. The recursive equation allows for efficient computation by only using the previous average and the new sample.
For a moving average of size ( N ), the recursive formula for the moving average can be described as follows:
[ \text{MA}n = \text{MA}{n-1} + \frac{x_n - \text{MA}_{n-1}}{N} ]
Where:
Here’s how you might implement this in C:
#include <stdio.h>
typedef struct {
double *buffer; // Buffer to hold the samples
double sum; // Sum of the samples
int size; // Size of the moving average
int index; // Current index in the buffer
int count; // Number of samples processed
} MovingAverage;
// Initialize the moving average filter
void initMovingAverage(MovingAverage *ma, int size) {
ma->buffer = (double *) malloc(size * sizeof(double));
ma->sum = 0.0;
ma->size = size;
ma->index = 0;
ma->count = 0;
}
// Update the moving average with a new sample
double updateMovingAverage(MovingAverage *ma, double newSample) {
// If we have filled the buffer, subtract the oldest sample
if (ma->count >= ma->size) {
// Subtract the sample that is going out of the window
ma->sum -= ma->buffer[ma->index];
} else {
// Increment the count until we reach the size
ma->count++;
}
// Add the new sample
ma->buffer[ma->index] = newSample;
ma->sum += newSample;
// Move the index forward and wrap around if necessary
ma->index = (ma->index + 1) % ma->size;
// Calculate and return the current moving average
return ma->sum / ma->count;
}
// Free the buffer memory
void freeMovingAverage(MovingAverage *ma) {
free(ma->buffer);
}
int main() {
int windowSize = 5; // Size of the moving average
MovingAverage ma;
// Initialize the moving average
initMovingAverage(&ma, windowSize);
// Simulated input samples
double samples[] = {10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0};
int numSamples = sizeof(samples) / sizeof(samples[0]);
// Process samples
for (int i = 0; i < numSamples; i++) {
double avg = updateMovingAverage(&ma, samples[i]);
printf("Sample: %.2f, Moving Average: %.2f\n", samples[i], avg);
}
// Clean up
freeMovingAverage(&ma);
return 0;
}
Structure Definition: MovingAverage structure holds the necessary variables to compute the moving average—such as the buffer for the samples, sum, size, current index, and count of samples.
Initialization Function: initMovingAverage initializes the filter with a specified size, allocating memory for the buffer.
Update Function: The updateMovingAverage function processes a new sample by computing the moving average:
Memory Cleanup: freeMovingAverage frees the allocated memory for the buffer.
Main Function: Simulates the process of feeding samples into the moving average filter and prints the results.
Ensure you link with the appropriate math library if needed and manage memory properly when using this in a larger application.
Not yet included in a published release.