Convert data to sfixed in workspace

2 次查看(过去 30 天)
Hey.
please, how can i convert the datatype of a variable in workspace from DOUBLE to Sfixed9_EN5.
the variables Lc(i) will be used as inputs of a simulink model.
Thank you.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global Lc1;
global Lc2;
global Lc3;
global Lc4;
global Lc5;
global Lc6;
global Lc7;
global Lc8;
global Lc9;
global Lc10;
R=1/2;
Mo=2;
numerrmin =1000;
SNRdB=1:0.5:3;
BER_bit=zeros(size(SNRdB));
codewordH=[0,0,0,0,0,0,0,0,0,0];
modulatedsig=1-2*codewordH;
for k=1:length(SNRdB)
fprintf('SNR:%d\n',SNRdB(k))
frame=0;
numerr=0;
rat_err=0;
EbN0_lin=10^(SNRdB(k)/10);
EsN0_lin=EbN0_lin*R*log2(Mo);
Es= 1 ;
N0=Es/EsN0_lin;
sigma=sqrt(N0/2);
while(numerr < numerrmin)
fprintf('frame : %d\n',frame)
frame = frame + 1;
bruit= randn(size(modulatedsig));
receivedsig =modulatedsig+sigma.*bruit;
Lci = (4.*(receivedsig./N0));
Lc1=Lci(1);
Lc2= Lci(2);
Lc3=Lci(3);
Lc4=Lci(4);
Lc5=Lci(5);
Lc6=Lci(6);
Lc7=Lci(7);
Lc8=Lci(8);
Lc9=Lci(9);
Lc10=Lci(10);
% Lc1=typecast(Lc1, 'fixdt(1,9,5)');
% Lc2=typecast(Lc2, 'sfix9');
% Lc3=typecast(Lc3, 'sfix9');
% Lc4=typecast(Lc4, 'sfix9');
% Lc5=typecast(Lc5, 'sfix9');
% Lc6=typecast(Lc6, 'sfix9');
% Lc7=typecast(Lc7, 'sfix9');
% Lc8=typecast(Lc8, 'sfix9');
% Lc9=typecast(Lc9, 'sfix9');
% Lc10=typecast(Lc10, 'sfix9');
Lc1=timeseries(Lc1);
Lc2=timeseries(Lc2);
Lc3=timeseries(Lc3);
Lc4=timeseries(Lc4);
Lc5=timeseries(Lc5);
Lc6=timeseries(Lc6);
Lc7=timeseries(Lc7);
Lc8=timeseries(Lc8);
Lc9=timeseries(Lc9);
Lc10=timeseries(Lc10);
%sim('LDPCMatlabSimulink','SrcWorkspace','current')
s=sim('LDPCMatlabSimulink');
x1=getdatasamples(x1,1);
x2=getdatasamples(x2,1);
x3=getdatasamples(x3,1);
x4=getdatasamples(x4,1);
x5=getdatasamples(x5,1);
x6=getdatasamples(x6,1);
x7=getdatasamples(x7,1);
x8=getdatasamples(x8,1);
x9=getdatasamples(x9,1);
x10=getdatasamples(x10,1);
SMS=[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10];%%%%%[0 0 0 0 0 0 0 0 0 0];%%
%iteration=it;
[Num1,Rat1] = biterr(codewordH,SMS);
numerr=numerr+Num1;
rat_err=rat_err+Rat1;
if frame>2
break ;
end
BER_bit(k)=rat_err/frame;
end
end
semilogy(SNRdB, BER_bit,'v-r','LineWidth',2);
grid on;
xlabel('SNR,(dB)')
ylabel('(BER)')

回答(1 个)

Akash
Akash 2024-7-8
编辑:Akash 2024-7-8
Hi Taamouch,
You can convert a variable 'x' of datatype 'double' to the fixed-point data type 'sfix9_En5' by using the 'fi' function as follows:-
x_fix = fi(x, 1, 9, 5);
In this function:
  • 1 specifies that the number is signed.
  • 9 is the word length.
  • 5 is the number of fractional bits.
For more detailed information, you can refer to the MATLAB documentation on the 'fi' function:-https://www.mathworks.com/help/fixedpoint/ref/embedded.fi.html#d126e96527:~:text=example-,a%20%3D%20fi(v%2Cs%2Cw%2Cf),-returns%20a%20fi
You can also utilize the following MATLAB documentation that demonstrates a model for converting signals between built-in and fixed-point data types:-

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by