OpenCores
URL https://opencores.org/ocsvn/wdsp/wdsp/trunk

Subversion Repositories wdsp

[/] [wdsp/] [trunk/] [sw/] [matlab-scripts/] [iir/] [gen_coeffs.m] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 parrado
%Header file generation for Wishbone compliant IIR core test
2
 
3
 
4
%Cleans workspace
5
clc;
6
clear all;
7
close all;
8
 
9
%Low-pass filter
10
%Cut-off frequencies
11
fs=8000;
12
fp1=425;
13
fp2=475;
14
fs1=410;
15
fs2=490;
16
 
17
%Ripples
18
Rp=3;
19
Rs=20;
20
 
21
%Normalized cut-off frequencies
22
Omp1=2*fp1/fs;
23
Omp2=2*fp2/fs;
24
Oms1=2*fs1/fs;
25
Oms2=2*fs2/fs;
26
 
27
 
28
%Filter order estimation
29
[N Omc]=buttord([Omp1 Omp2],[Oms1 Oms2],Rp,Rs);
30
 
31
%Filter design
32
[b_d a_d]=butter(N,Omc);
33
 
34
 
35
%Bits to use for each coefficient
36
Nbits=16;
37
 
38
 
39
%SOS structure
40
[SOS g] = tf2sos(b_d,a_d);
41
 
42
 
43
%Number of sections
44
Nsect=size(SOS,1);
45
 
46
%Gain for each section
47
gk=g^(1/Nsect);
48
 
49
 
50
%Fractional part
51
Q=floor(Nbits-1-log2(max([gk max(abs(SOS))])));
52
 
53
%Quantized coefficients
54
SOS_Q=round(SOS*2^Q);
55
 
56
%Quantized gain
57
gk_Q=round(gk*2^Q);
58
 
59
%Auxiliary variable
60
SOS_orig=SOS_Q;
61
 
62
 
63
%Header file generation
64
SOS=reshape(fliplr(SOS)',Nsect*3*2,1);
65
SOS_Q=reshape(fliplr(SOS_Q)',Nsect*3*2,1);
66
SOS_fprintf=[SOS_Q'; SOS'];
67
handle = fopen('../../IIR6/coefs_sos.h', 'wt');
68
fprintf(handle, '/*Second Order Sections (SOS) automatically generated header file*/\n');
69
fprintf(handle, '/*M.Eng. Alexander López Parrado*/\n\n');
70
fprintf(handle, '#include <stdint.h>\n');
71
fprintf(handle, '/*The number of sections*/\n');
72
fprintf(handle, '#define NSECT %d\n\n',Nsect);
73
fprintf(handle, '/*Number of bits in fractional part of coeffcients*/\n');
74
fprintf(handle, '/*Fixed point format with %d bits ([%d].[%d])*/\n',Nbits,Nbits-Q,Q);
75
fprintf(handle, '#define Q %d\n\n',Q);
76
 
77
fprintf(handle, '/*Gain on each stage*/');
78
fprintf(handle, '\nconst int16_t gk = %d;\n\n',gk_Q);
79
fprintf(handle, '/*Filter Coefficients ,b10,b11,b12,a10,a11,a12,b00,b01,b02,a00,a01,a02*/');
80
fprintf(handle, '\nconst int16_t SOS[NSECT*3*2] = {\n');
81
 
82
fprintf(handle, '%d, //%f\n', SOS_fprintf);
83
 
84
fprintf(handle, '};\n');
85
fclose(handle);
86
 
87
 
88
 
89
 
90
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.