====== Návrh a realizace FIR filtru ======
[[http://www.flickr.com/photos/pitel/5468520017|{{ http://farm6.static.flickr.com/5058/5468520017_39b43b07c9_z.jpg |FIR filter}}]]
CC=gcc
RM=rm -f
CFLAGS=-O2 -pipe -pedantic-errors -Wall -Wextra -march=native -fomit-frame-pointer -std=c99
all: gen fir
.PHONY: clean
gen: gen.c
$(CC) $(CFLAGS) -lm -D_GNU_SOURCE $< -o $@
fir: fir.c
$(CC) $(CFLAGS) $< -o $@
clean:
$(RM) *.raw fir gen
run: all
./gen | tee original.raw | ./fir > filtered.raw
plot: run
gnuplot plot.gnu
#include
#include
#include
#define SR 44100
#define freq1 1000
#define freq2 10000
#define samples 100000
int main(/*int argc, char *argv[]*/) {
for (size_t sample = 0; sample < samples; sample++) {
printf ("%f\n", sin(2 * M_PI * freq1 * sample / SR) / 2 + sin(2 * M_PI * freq2 * sample / SR) / 2);
}
return EXIT_SUCCESS;
}
#include
#include
#include
int main() {
const float coeff[] = {.1, .1, .09, .09, .08, .06, .05, .04, .02, .01, 0, -.01, -.02, -.02, -.02, -.02, -.02, -.02, -.01, -.01, 0, 0, .01, .01, .01, .01, .01, .01, .01, 1}; //Jednicka je zarazka!
size_t coefs = 0;
while (coeff[++coefs] < 1); //Pocet koeficientu
float history[coefs];
bzero(&history, coefs * sizeof(float)); //Vynulovani
float x;
while (scanf("%f", &x) != EOF) {
//printf("%f\n", x);
//Posun prvku v poli
for (size_t i = coefs; i > 0; i--) {
history[i] = history[i - 1];
}
history[0] = x;
//printa(history, coefs);
//FIR
float out = 0;
for (size_t i = 0; i < coefs; i++) {
out += history[i] * coeff[i];
}
printf("%f\n", out);
}
return EXIT_SUCCESS;
}
plot [0:500] "original.raw" with lines, "filtered.raw" with lines
pause -1