在对vivado中的rom ip核进行初始化的时候,需要coe文件。在官方文档pg058中关于初始化文件的描述如下图
这里给出MATLAB代码供学习讨论。
clear all;close all; clc;
%%
% 功能:稍加修改可产生任意波形、深度、宽度的coe文件
%% 波形产生
B = 300e6;
T = 10e-6;
fs = 480e6; ts = 1/fs;
tt = -T/2+ts/2:ts:T/2-ts/2;
lfm = exp(1j*pi*B*tt.^2/T); %线性调频信号
depth = 65536; %rom核深度的1/2,复数
s1 = zeros(1,depth);
s2 = zeros(1,depth);
m = length(lfm);
s1(1:m) = real(lfm);
s2(1:m) = imag(lfm);
%% 量化到0-32767 unsigned int
width = 16; %rom核的宽度
lfmrefix = fix(s1*(2^(width-1)-1)); %量化 16 --- 2^15-1=32767
for k = 1:depth
if(lfmrefix(k) < 0)
lfmrefix(k) = lfmrefix(k) + 2^width; %负数取补码 X[补码]=2.^N + X, X为十进制数
end
end
lfmimfix = fix(s2*(2^(width-1)-1));
for k = 1:depth
if(lfmimfix(k) < 0)
lfmimfix(k) = lfmimfix(k) + 2^width;
end
end
lfmfix = [lfmrefix lfmimfix];
figure
subplot(211)
plot(s1)
subplot(212)
plot(s2,'r')
%% 文件写入
fiddscoe = fopen('lfm1.coe','w');
aa = 'memory_initialization_radix = 16;';
bb = 'memory_initialization_vector =';
cc = 13;%回车键,回车会使光标回到行首
cdscoe = fwrite(fiddscoe,aa,'uchar');
cdscoe = fwrite(fiddscoe,cc,'uchar');
cdscoe = fwrite(fiddscoe,bb,'uchar');
cdscoe = fwrite(fiddscoe,cc,'uchar');
hex_lfmfix = dec2hex(lfmfix,4);
for k = 1:depth*2
cdscoe = fwrite(fiddscoe,hex_lfmfix(k,1:4),'uchar');
cdscoe = fwrite(fiddscoe,',','uchar');
cdscoe = fwrite(fiddscoe,cc,'uchar');
end
cdscoe = fwrite(fiddscoe,';','uchar');
status = fclose(fiddscoe);
clear all;close all; clc;
%% 波形产生
B = 300e6;
T = 10e-6;
fs = 480e6;
ts = 1/fs;
tt = -T/2+ts/2:ts:T/2-ts/2;
lfm = exp(1j*pi*B*tt.^2/T); %线性调频信号
match = conj(lfm).*hanning(length(lfm)).';
match_fft = fft(match,65536);
match_fft1 = match_fft./(max(abs(match_fft))); %归一化
s1 = real(match_fft1);
s2 = imag(match_fft1);
figure
subplot(211)
plot(s1)
subplot(212)
plot(s2)
%% 量化到0-32767 unsigned int
width = 16; %rom核的宽度
matchrefix = fix(s1*(2^(width-1)-1)); %量化 16 --- 2^15-1=32767
for k = 1:65536
if(matchrefix(k) < 0)
matchrefix(k) = matchrefix(k) + 2^width;%负数取补码 X[补码]= X + 2.^N, X为十进制数
end
end
matchimfix = fix(s2*(2^(width-1)-1));
for k = 1:65536
if(matchimfix(k) < 0)
matchimfix(k) = matchimfix(k) + 2^width;
end
end
matchfix = [matchrefix matchimfix];
%% 文件写入
fiddscoe = fopen('match1.coe','w');
aa = 'memory_initialization_radix = 16;';
bb = 'memory_initialization_vector =';
cc = 13;%回车键,回车会使光标回到行首
cdscoe = fwrite(fiddscoe,aa,'uchar');
cdscoe = fwrite(fiddscoe,cc,'uchar');
cdscoe = fwrite(fiddscoe,bb,'uchar');
cdscoe = fwrite(fiddscoe,cc,'uchar');
hex_matchfix = dec2hex(matchfix,4);
for k = 1:131072
cdscoe = fwrite(fiddscoe,hex_matchfix(k,1:4),'uchar');
cdscoe = fwrite(fiddscoe,',','uchar');
cdscoe = fwrite(fiddscoe,cc,'uchar');
end
cdscoe = fwrite(fiddscoe,';','uchar');
status = fclose(fiddscoe);