Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implement Moving average filter using C

Similar presentations


Presentation on theme: "Implement Moving average filter using C"— Presentation transcript:

1 Implement Moving average filter using C
29th October

2 Today’s topic Implement very simple FIR filter using C
N-tap Moving average filter. Listen before/after filtering Make sure whether the filtered signal is high-frequency cutoff Extended works Replace filter coefficients Implement a FIR filter with arbitrary frequency response (using MATLAB)

3 Implementing FIR filter
1 2 y[n-1] y[0] y[n] y[1] y[n+1] y[2] N/2 보다 작은 영역과 마지막 부분을 고려해야 함.

4 Extra study File의 전체 길이를 알아내는 방법 “stdio.h” 에 정의된 FILE의 structure를 알면 됨
Length와 연관된 member가 존재 FILE 과 관련된 I/O 함수를 이용하는 방법 fseek(*file_pointer,off_set,arribute) 주어진 File pointer에서 offset만큼 이동 시킴 Attribute : SEEK_CUR, SEEK_SET, SEEK_END ftell(*file_pointer) 현재 file pointer의 위치를 넘겨줌 rewind(*file_pointer) 파일을 첫번째 위치로 되돌림

5 File 의 길이를 알아내는 방법 fseek(fpr,0L,SEEK_END);
File pointer를 끝 부분으로 이동 len=ftell(fpr)/sizeof(short); 현재 File pointer의 위치를 읽고 Element size로 나누어줌 결국, 전체 element의 개수가 얻어짐 rewind(fpr); File pointer를 시작 위치로 다시 이동

6 Moving average filter의 C-코드
main(int argc, char **argv){ if ( argc<4 ) { printf("usage : %s input output num_tap\n",argv[0]); exit(-1); } if ((fpr=fopen(argv[1],"rb"))==NULL) { printf("Input file [%s] is not found.\n",argv[1]); fpw=fopen(argv[2],"wb"); num_tap=atoi(argv[3]); fseek(fpr,0L,SEEK_END); len=ftell(fpr)/sizeof(short); rewind(fpr); in=(short *)malloc(sizeof(short)*len); out=(short *)malloc(sizeof(short)*len); fread(in,sizeof(short),len,fpr); for(i=0; i<len; i++) { t_val=0.; for(j=-num_tap/2; j<=num_tap/2; j++) { if ( i+j >= 0 && i+j < len ) t_val+=(1./num_tap)*in[i+j]; out[i]=t_val; fwrite(out,sizeof(short),len,fpw); free(out); free(in); fclose(fpr); fclose(fpw);

7 Core part for(i=0; i<len; i++) { t_val=0.;
for(j=-num_tap/2; j<=num_tap/2; j++) { if ( i+j >= 0 && i+j < len ) t_val+= (1./num_tap)*in[i+j]; } out[i]=t_val; FIR filter coefficients

8 Homework Get filter coefficients using MATLAB
Using MATLAB Filter Design Tools Replace the filter coefficients in the example code with those from MATLAB Listen the filtered signals Due :12, Nov

9 MATLAB Digital filter design tool

10 Export to C-code To C-code


Download ppt "Implement Moving average filter using C"

Similar presentations


Ads by Google