数据导入csv到转十六进制处理

clc;
clear all;
M=3;
N=4;
points = 1.0e6;
sample_t3 = 2.0e-7;%单位s,fs = 5(Mhz);
per_mm = 1500/360;
%% 25500
L = 800;
X1_1 = 20+2506+4884;
Y1_1 = 6*L+X1_1;
X1_2 = Y1_1;
Y1_2 = 6*L+X1_2;
X1_3 = Y1_2;
Y1_3 = 6*L+X1_3;
%filename = 'D:\...\.csv';
A_in_1 = csvread(filename,X1_1,M,[X1_1,M,Y1_1-1,N]);
A_in_2 = csvread(filename,X1_2,M,[X1_2,M,Y1_2-1,N]);
A_in_3 = csvread(filename,X1_3,M,[X1_3,M,Y1_3-1,N]);
filename = 'D:\...\.csv';
A_out_1 = csvread(filename,X1_1,M,[X1_1,M,Y1_1-1,N]);
A_out_2 = csvread(filename,X1_2,M,[X1_2,M,Y1_2-1,N]);
A_out_3 = csvread(filename,X1_3,M,[X1_3,M,Y1_3-1,N]);
n=1:20:length(A_in_1);
A_1=1:240;
A_in_1=A_in_1(n,2);
A_in_2=A_in_2(n,2);
A_in_3=A_in_3(n,2);
A_out_1 = A_out_1(n,2);
A_out_2 = A_out_2(n,2);
A_out_3 = A_out_3(n,2);
%抽取数据画图
figure(1);
subplot(3,1,1);
yyaxis left; % 激活左边的轴
ylim([0.4,2.1]); % 设置左y轴的界限
yyaxis right; % 激活右边的轴
ylim([0.4,2.1]); % 设置左y轴的界限
plot(A_1',A_in_1(:,1)','r',A_1',A_out_1(:,1)','b');
legend('in','out'); 
%%
Radix       = 16;
Coeff_width = 15;
comple_A_in_1 = zeros(1,240);
comple_A_in_2 = zeros(1,240);
comple_A_in_3 = zeros(1,240);
comple_A_out_1 = zeros(1,240);
comple_A_out_2 = zeros(1,240);
comple_A_out_3 = zeros(1,240);
% convert the fraction number to integer
% 20 bits with 1 signed bit
% M_mul = round(M.*2^(Coeff_width-1));  
%%
%数据量化
A_in_1_mul = round(A_in_1.*2^(Coeff_width-1));  
LENGTH_M = length(A_in_1_mul);
% convert the coefficient to complement format
for i = 1: LENGTH_M
    if(A_in_1_mul(i) < 0)
        comple_A_in_1(i) = 2^Coeff_width + A_in_1_mul(i);
    else
        comple_A_in_1(i) = A_in_1_mul(i);
    end
end
hex_A_in_1 = dec2hex(comple_A_in_1,4);

A_in_2_mul = round(A_in_2.*2^(Coeff_width-1));  
LENGTH_M = length(A_in_2_mul);
% convert the coefficient to complement format
for i = 1: LENGTH_M
    if(A_in_2_mul(i) < 0)
        comple_A_in_2(i) = 2^Coeff_width + A_in_2_mul(i);
    else
        comple_A_in_2(i) = A_in_2_mul(i);
    end
end
hex_A_in_2 = dec2hex(comple_A_in_2,4);

A_in_3_mul = round(A_in_3.*2^(Coeff_width-1));  
LENGTH_M = length(A_in_3_mul);
% convert the coefficient to complement format
for i = 1: LENGTH_M
    if(A_in_3_mul(i) < 0)
        comple_A_in_3(i) = 2^Coeff_width + A_in_3_mul(i);
    else
        comple_A_in_3(i) = A_in_3_mul(i);
    end
end
hex_A_in_3 = dec2hex(comple_A_in_3,4);

A_out_1_mul = round(A_out_1.*2^(Coeff_width-1));  
for i = 1: LENGTH_M
    if(A_out_1_mul(i) < 0)
        comple_A_out_1(i) = 2^Coeff_width + A_out_1_mul(i);
    else
        comple_A_out_1(i) = A_out_1_mul(i);
    end
end
hex_A_out_1 = dec2hex(comple_A_out_1,4);

A_out_2_mul = round(A_out_2.*2^(Coeff_width-1));  
for i = 1: LENGTH_M
    if(A_out_2_mul(i) < 0)
        comple_A_out_2(i) = 2^Coeff_width + A_out_2_mul(i);
    else
        comple_A_out_2(i) = A_out_2_mul(i);
    end
end
hex_A_out_2 = dec2hex(comple_A_out_2,4);

A_out_3_mul = round(A_out_3.*2^(Coeff_width-1));  
for i = 1: LENGTH_M
    if(A_out_3_mul(i) < 0)
        comple_A_out_3(i) = 2^Coeff_width + A_out_3_mul(i);
    else
        comple_A_out_3(i) = A_out_3_mul(i);
    end
end
hex_A_out_3 = dec2hex(comple_A_out_3,4);
%%
%输出数据文本
fid=fopen('D:\m_code\hex_A_in_3.txt','w'); %创建y.coe文件
for i=1:length(hex_A_out_2)
    if(i < length(hex_A_out_2))
        for j = 1:4         
            fprintf(fid,'%s',hex_A_in_3(i,j)); %向y.coe中写入数据
        end
        fprintf(fid,',\n');
    else
        for j = 1:4
            fprintf(fid,'%s',hex_A_in_3(i,j));
        end
        fprintf(fid,';\n');
    end
end
fclose(fid);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值