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
- Assistant
assume C language programmer, show exemplar code for moving average filter and show frequency and phase respond
Certainly! A moving average filter is a commonly used digital filter that can smooth a signal by averaging a set of samples. If we implement a simple moving average filter in C, we can also visualize its frequency and phase response using tools such as MATLAB, Python, or others.
Here's a simple implementation of a moving average filter in C:
#include <stdio.h>
#include <stdlib.h>
#define N 5 // Size of the moving window
void moving_average_filter(double* input, double* output, int length) {
double sum = 0.0;
int i, j;
for (i = 0; i < length; i++) {
sum = 0.0;
// Sum the samples within the moving window
for (j = 0; j < N; j++) {
if (i - j >= 0) {
sum += input[i - j];
}
}
// Calculate the average
output[i] = sum / ((i < N) ? (i + 1) : N);
}
}
int main() {
// Example input signal (for demonstration)
double input[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
int length = sizeof(input)/sizeof(input[0]);
double* output = (double*)malloc(length * sizeof(double));
// Apply moving average filter
moving_average_filter(input, output, length);
// Print the output
printf("Input Signal:\n");
for (int i = 0; i < length; i++) {
printf("%f ", input[i]);
}
printf("\n\nFiltered Output (Moving Average):\n");
for (int i = 0; i < length; i++) {
printf("%f ", output[i]);
}
printf("\n");
// Clean up
free(output);
return 0;
}
Filter Size (N): The size of the moving average window is defined with #define N 5. You can change this value to change the filter's characteristics.
Function Definition: The moving_average_filter() function takes an input signal, applies the moving average, and outputs the filtered signal.
Main Function: In the main function, we create a sample input signal and allocate memory for the output. The filter function is then called, and both the input and output are printed.
To visualize the frequency and phase response of the moving average filter, you would typically plot its response using tools like Python with matplotlib and scipy. Here’s how you can do this using Python:
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import freqz
# Filter parameters
N = 5 # Moving average window size
b = np.ones(N) / N # Coefficients for a moving average filter
a = 1 # Denominator coefficients
# Frequency response
w, h = freqz(b, a, worN=8000)
# Plotting
plt.figure()
plt.subplot(2, 1, 1)
plt.plot(0.5 * np.pi * w / np.pi, np.abs(h), 'b')
plt.title('Frequency Response')
plt.xlabel('Normalized Frequency (π rad/sample)')
plt.ylabel('Magnitude')
plt.grid()
plt.subplot(2, 1, 2)
plt.plot(0.5 * np.pi * w / np.pi, np.angle(h), 'b')
plt.title('Phase Response')
plt.xlabel('Normalized Frequency (π rad/sample)')
plt.ylabel('Phase (radians)')
plt.grid()
plt.tight_layout()
plt.show()
Filter Coefficients: The filter coefficients for the moving average are defined as b = np.ones(N) / N.
freqz Function: The freqz function is used to compute the frequency response of the filter.
Plotting: The code generates two plots: one for magnitude and another for phase response.
This should give you a clear idea of how to implement a moving average filter in C and how to visualize its frequency and phase response in Python. Make sure you have the necessary libraries installed (numpy, matplotlib, scipy) to run the Python script.
Not yet included in a published release.