assume C language programmer, show exemplar code for moving average filter and show…
- User
assume C language programmer, show exemplar code for moving average filter and show frequency and phase respond in SciLab
- Assistant
assume C language programmer, show exemplar code for moving average filter and show frequency and phase respond in SciLab
Certainly! In C, a moving average filter can be implemented using an array to store samples and a loop to compute the average of the last N samples. Below is an example of a simple moving average filter in C, followed by a method to visualize the frequency and phase response in SciLab.
The example applies a moving average filter on an input signal. In this implementation, we'll compute the average of the last N samples:
#include <stdio.h>
#include <stdlib.h>
void moving_average_filter(double *input, double *output, int length, int N) {
double sum = 0.0;
for (int i = 0; i < length; i++) {
sum += input[i];
if (i < N) {
output[i] = sum / (i + 1); // Average for less than N samples
} else {
sum -= input[i - N]; // Subtract the element going out of the window
output[i] = sum / N; // Compute the moving average
}
}
}
int main() {
int N = 5; // Size of moving average
int length = 100; // Length of the signal
double *input = (double *)malloc(length * sizeof(double));
double *output = (double *)malloc(length * sizeof(double));
// Generate a sample input signal (for example: a sine wave)
for (int i = 0; i < length; i++) {
input[i] = sin(2 * 3.14159 * i / 10);
}
// Apply moving average filter
moving_average_filter(input, output, length, N);
// Print output
for (int i = 0; i < length; i++) {
printf("%d: Input=%.3f, Output=%.3f\n", i, input[i], output[i]);
}
free(input);
free(output);
return 0;
}
To visualize the frequency and phase responses of the moving average filter, you can run the following SciLab code. This code takes the coefficients of the moving average filter and computes its frequency response using the freqs function.
Make sure you have the required toolboxes in SciLab to perform the operations.
// SciLab Code for Frequency Response of Moving Average Filter
// Parameters
N = 5; // Size of the moving average
b = ones(1, N) / N; // Moving average filter coefficients
a = 1; // Denominator coefficients
// Frequency response
[w, H] = freqs(b, a, linspace(0, %pi, 512));
// Plot the magnitude response
clf;
subplot(2, 1, 1);
plot(w, abs(H));
title('Magnitude Response');
xlabel('Frequency (radians/sample)');
ylabel('|H(e^{j\omega})|');
grid();
// Plot the phase response
subplot(2, 1, 2);
plot(w, atan2(imag(H), real(H)));
title('Phase Response');
xlabel('Frequency (radians/sample)');
ylabel('Phase (radians)');
grid();
C Code:
moving_average_filter to compute the moving average.SciLab Code:
b represents the moving average filter coefficients (equal values summing to 1).freqs computes the frequency response based on the filter coefficients.This should give you a complete cycle from C implementation to frequency response analysis in SciLab!
Not yet included in a published release.