Kalábovi

Kalábovic wikina

Uživatelské nástroje

Nástroje pro tento web


pitel:mul:cv1

Návrh a realizace FIR filtru

FIR filter

Makefile
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 [email protected]
 
fir: fir.c
	$(CC) $(CFLAGS) $< -o [email protected]
 
clean:
	$(RM) *.raw fir gen
 
run: all
	./gen | tee original.raw | ./fir > filtered.raw
 
plot: run
	gnuplot plot.gnu
gen.c
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
 
#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;
}
fir.c
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
 
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.gnu
plot [0:500] "original.raw" with lines, "filtered.raw" with lines
pause -1
/var/www/wiki/data/pages/pitel/mul/cv1.txt · Poslední úprava: 03. 07. 2012, 13.53:39 (upraveno mimo DokuWiki)