一. COE文件格式:
1. Block Memory COE File
; This .COE file specifies the contents for a block memory of depth=12, and width=4.
memory_initialization_radix=2;
memory_initialization_vector=
1111,
1111,
1111,
1111,
0000,
0101,
0011,
0000,
1111,
1111,
1111,
1111;
2. FIR coe
; Example of a Distributed Arithmetic (DA) FIR Filter .COE file
; with hex coefficients, 8 symmetrical taps, and 12-bit coefficients.
Radix = 16;
CoefData= 346, EDA, 0D6, F91, F91, 0D6, EDA, 346;
二. MATLAB产生代码:
1. 量化
以16位量化为例:最大值为2^15-1;
huibo=max(abs(huibo));
huibo_qua=round(huibo*(2^(16-1)-1));
2. IIR滤波器系数量化时将α(1)变成2的整次数倍
m=max(max(abs(a),abs(b)));
Qm=floor(log2(m/a(1)));
if Qm<log2(m/a(1))
Qm=Qm+1;
end
Qm=2^Qm;
Qb=round(b/Qm*(2^(16-1)-1));
Qa=round(a/Qm*(2^(16-1)-1));
3. 制作文件
fid=fopen('.\huibo.coe','w');
fprintf(fid,'memory_initialization_radix=2;\n');
fprintf(fid,'memory_initialization_vector=\n');
for k=1:length(huibo_qua)
B_s=dec2bin(huibo_qua(k)+((huibo_qua(k))<0)*2^16,16);
for j=1:16
if B_s(j)=='1'
tb=1;
else
tb=0;
end
fprintf(fid,'%d',tb);
end
if k == length(huibo_qua)
fprintf(fid,'%s',';');
else
fprintf(fid,'%s\r\n',',');
end
end
fclose(fid);