fir
This commit is contained in:
parent
df11e34235
commit
53e51ca468
1 changed files with 42 additions and 0 deletions
42
fir.c
Normal file
42
fir.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define N 5 // Number of taps (filter coefficients)
|
||||
|
||||
// Example filter coefficients (simple low-pass averaging filter)
|
||||
float coeffs[N] = {
|
||||
0.2, 0.2, 0.2, 0.2, 0.2 // Equal weights → moving average filter
|
||||
};
|
||||
|
||||
float buffer[N] = {0}; // Circular buffer to store recent inputs
|
||||
int bufIndex = 0;
|
||||
|
||||
float fir_filter(float input) {
|
||||
buffer[bufIndex] = input;
|
||||
|
||||
float output = 0.0;
|
||||
int index = bufIndex;
|
||||
|
||||
// Apply the convolution (dot product of input buffer and coefficients)
|
||||
for (int i = 0; i < N; ++i) {
|
||||
output += coeffs[i] * buffer[index];
|
||||
if (--index < 0)
|
||||
index = N - 1; // Wrap around circular buffer
|
||||
}
|
||||
|
||||
bufIndex = (bufIndex + 1) % N;
|
||||
return output;
|
||||
}
|
||||
|
||||
int main() {
|
||||
float noisy_signal[] = {1, 2, 3, 2,
|
||||
100, 3, 2, 1}; // A spike in the middle (100)
|
||||
int len = sizeof(noisy_signal) / sizeof(float);
|
||||
|
||||
printf("Filtered output:\n");
|
||||
for (int i = 0; i < len; ++i) {
|
||||
float filtered = fir_filter(noisy_signal[i]);
|
||||
printf("%.2f\n", filtered);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue