How to convert this MATLAB code to HDL using HDL Coder
5 次查看(过去 30 天)
显示 更早的评论
clc; % Define parameter Blocksize = 52; Idepth = 13;
h0 = 1; h1 = 1; h2 = 1; h3 = 1;
n0 = 0; n1 = 0; n2 = 0; n3 = 0; %Convolution code t = poly2trellis([7],[171 133]); d = [0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 1 0 1]; c = convenc(d,t); %Interleaver d_intlr = interleaver(c, Blocksize, 1, Idepth, 0); %16QAM d_qam = zeros(1,14); a = 1; for i = 1: Idepth d_qam(i)= mapping(d_intlr(a:a+3)); a = a+4; end %Alamouti Encoder s = d_qam; s0 = ones(1,length(s)); s1 = ones(1,length(s)); temp = ones(1:2); r0 = zeros(1,(length(s)/2)); r1 = zeros(1,(length(s)/2)); r2 = zeros(1,(length(s)/2)); r3 = zeros(1,(length(s)/2)); a = 1; b = 1; for i=1:length(s) if a == 1 temp(1) = s(i); a = a+1; elseif a == 2 temp(2) = s(i); a = 1; s0(b) = temp(1); s1(b) = temp(2); s0(b+1) = -conj(temp(2)); s1(b+1) = conj(temp(1)); b = b+2; end end %IFFT s0_ifft = 32*ifft(s0); s1_ifft = 32*ifft(s1); %Channel a = 1; b = 1; for i=1:length(s) if a==1 r0(b) = h0*s0_ifft(i) + h1*s1_ifft(i) + n0; r2(b) = h2*s0_ifft(i) + h3*s1_ifft(i) + n2; a = a+1; elseif a==2 r1(b) = h0*s0_ifft(i) + h1*s1_ifft(i) + n1; r3(b) = h2*s0_ifft(i) + h3*s1_ifft(i) + n3; a = 1; b = b+1; end end c0 = zeros(1, length(s)); c1 = zeros(1, length(s)); a = 1; b = 1; for i=1:length(s) if a==1 c0(i) = r0(b); c1(i) = r2(b); a = a+1; elseif a==2 c0(i) = r1(b); c1(i) = r3(b); a = 1; b = b+1; end end %FFT c0_fft = fft(c0)/32; c1_fft = fft(c1)/32; %Alamouti Decoder s00 = zeros(1, length(s)/2); s11 = zeros(1, length(s)/2); a = 1; for i=1:length(s)/2 s00(i) = conj(h0)*c0_fft(a) + h1*conj(c0_fft(a+1)) + conj(h2)*c1_fft(a) + h3*conj(c1_fft(a+1)); s11(i) = conj(h1)*c0_fft(a) - h0*conj(c0_fft(a+1)) + conj(h3)*c1_fft(a) - h2*conj(c1_fft(a+1)); a = a+2; end s_out = ones(1,length(s)); a = 1; b = 1; for i=1:length(s) if a == 1 s_out(i) = s00(b); a = a + 1; elseif a == 2 s_out(i) = s11(b); a = 1; b = b + 1; end end s_out = s_out/4; %D-QAM
d_D-QAM = zeros(1,52); a = 1; for i = 1: Idepth d_D-QAM(a:a+3) = demap(round(s_out(i))); a = a+4; end %Deinterleaver d_dintlr = Deinterleaver(d_D-QAM, Blocksize, 1, Idepth, 0); tb = 2; %Viterbi Decoder deco = vitdec(d_dintlr,t,tb,'trunc', 'hard');
%%%%%%%%%%%%%%%%%OFDM example%%%%%%%%%%% %this is a simple exam code for OFDM modulation and demodulation %source data clear all close all SNR = [1:2:30]; snr = 10.^(SNR/10); BER = zeros(1, length(snr)); for l = 1:length(snr) n = 2^16; %bits M = 16; %QAM modulation x = randint(1,n,M); c = 64; %subcarrier dB = 20; %AWGN SNR %convert the sequential data into parallel form x_vec = reshape(x,c,n/c); xqam = modem.qammod(M); %apply 16-QAM modulation x_mod = modulate(xqam,x_vec); %take IFFT of each subcarrier x_ifft = ifft(x_mod); % scatterplot(x_mod(1,:)); % scatterplot(x_ifft(1,:)); %without noise %y = x_ifft; %transmit signal through AWGN channel y = awgn(x_ifft,SNR(l),'measured'); %take FFT of each subcarrier y_fft = fft(y); %plot the received signal vector in subarrier no. 1 % scatterplot(y_fft(1,:)); %demodulate to recover the transmit signal y_vec = demodulate(modem.qamdemod(M),y_fft); % scatterplot(y_vec(1,:)); %recombine it into a seqeuential data y_seq = reshape(y_vec,1,n); %check symbol error rate [num1,ser]= symerr(x_vec,y_vec) %check bit error rate [num2,BER(l)]= biterr(x,y_seq,10) end semilogy(SNR,BER) xlabel('SNR [dB]') ylabel('BER') axis([0 SNR(length(SNR)) 1e-5 .5]) grid on
function y = interleaver(x, Block_Size, N_BPSC, I_DEPTH, D) k = 0:Block_Size-1; s = max(N_BPSC/2, 1); i = (Block_Size/I_DEPTH)*mod(k, I_DEPTH) + floor(k/I_DEPTH); j = s*floor(i/s) + mod( i+Block_Size-floor(I_DEPTH*i/Block_Size) , s); output_index = mod( j+Block_Size-N_BPSC*D , Block_Size) NUM_of_CYCLE = length(x) / Block_Size; for cycle = 0:NUM_of_CYCLE-1; temp(1:Block_Size) = x(cycle*Block_Size+1 : (cycle+1)*Block_Size); for index = 1:Block_Size; temp_intrlv(index) = temp(output_index(index)+1); end y(cycle*Block_Size+1 : (cycle+1)*Block_Size) = temp_intrlv(1:Block_Size); end end
function y = Deinterleaver(x, Block_Size, N_BPSC, I_DEPTH, D) k = 0:Block_Size-1; s = max(N_BPSC/2, 1); i = (Block_Size/I_DEPTH)*mod(k, I_DEPTH) + floor(k/I_DEPTH); j = s*floor(i/s) + mod( i+Block_Size-floor(I_DEPTH*i/Block_Size) , s); output_index = mod( j+Block_Size-N_BPSC*D , Block_Size); NUM_of_CYCLE = length(x) / Block_Size; for cycle = 0:NUM_of_CYCLE-1; temp(1: Block_Size) = x(cycle*Block_Size+1 : (cycle+1)*Block_Size); for index = 1:Block_Size; temp_deintrlv(output_index(index)+1) = temp(index); end y(cycle*Block_Size+1: (cycle+1)*Block_Size) = temp_deintrlv(1:Block_Size); end end
function y = mapping(x) if (x(1)==0) && (x(2)==0) && (x(3)==0) && (x(4)==0) y = -3 - 3*i; elseif (x(1)==0) && (x(2)==0) && (x(3)==0) && (x(4)==1) y = -3 - 1*i; elseif (x(1)==0) && (x(2)==0) && (x(3)==1) && (x(4)==0) y = -3 + 3*i; elseif (x(1)==0) && (x(2)==0) && (x(3)==1) && (x(4)==1) y = -3 + 1*i; elseif (x(1)==0) && (x(2)==1) && (x(3)==0) && (x(4)==0) y = -1 - 3*i; elseif (x(1)==0) && (x(2)==1) && (x(3)==0) && (x(4)==1) y = -1 - 1*i; elseif (x(1)==0) && (x(2)==1) && (x(3)==1) && (x(4)==0) y = -1 + 3*i; elseif (x(1)==0) && (x(2)==1) && (x(3)==1) && (x(4)==1) y = -1 + 1*i; elseif (x(1)==1) && (x(2)==0) && (x(3)==0) && (x(4)==0) y = 3 - 3*i; elseif (x(1)==1) && (x(2)==0) && (x(3)==0) && (x(4)==1) y = 3 - 1*i; elseif (x(1)==1) && (x(2)==0) && (x(3)==1) && (x(4)==0) y = 3 + 3*i; elseif (x(1)==1) && (x(2)==0) && (x(3)==1) && (x(4)==1) y = 3 + 1*i; elseif (x(1)==1) && (x(2)==1) && (x(3)==0) && (x(4)==0) y = 1 - 3*i; elseif (x(1)==1) && (x(2)==1) && (x(3)==0) && (x(4)==1) y = 1 - 1*i; elseif (x(1)==1) && (x(2)==1) && (x(3)==1) && (x(4)==0) y = 1 + 3*i; elseif (x(1)==1) && (x(2)==1) && (x(3)==1) && (x(4)==1) y = 1 + 1*i; end end
function x = demap(y) if y == -3 + 3*i x(1)=0; x(2)=0; x(3)=1; x(4)=0; elseif y == -3 + 1*i x(1)=0; x(2)=0; x(3)=1; x(4)=1; elseif y == -3 - 3*i x(1)=0; x(2)=0; x(3)=0; x(4)=0; elseif y == -3 - 1*i x(1)=0; x(2)=0; x(3)=0; x(4)=1; elseif y == -1 + 3*i x(1)=0; x(2)=1; x(3)=1; x(4)=0; elseif y == -1 + 1*i x(1)=0; x(2)=1; x(3)=1; x(4)=1; elseif y == -1 - 3*i x(1)=0; x(2)=1; x(3)=0; x(4)=0; elseif y == -1 - 1*i x(1)=0; x(2)=1; x(3)=0; x(4)=1; elseif y == 3 + 3*i x(1)=0; x(2)=1; x(3)=0; x(4)=1; elseif y == 3 + 1*i x(1)=1; x(2)=0; x(3)=1; x(4)=1; elseif y == 3 - 3*i x(1)=1; x(2)=0; x(3)=0; x(4)=0; elseif y == 3 - 1*i x(1)=1; x(2)=0; x(3)=0; x(4)=1; elseif y == 1 + 3*i x(1)=1; x(2)=1; x(3)=1; x(4)=0; elseif y == 1 + 1*i x(1)=1; x(2)=1; x(3)=1; x(4)=1; elseif y == 1 - 3*i x(1)=1; x(2)=1; x(3)=0; x(4)=0; elseif y == 1 - 1*i x(1)=1; x(2)=1; x(3)=0; x(4)=1; end end
0 个评论
回答(1 个)
Tim McBrayer
2013-8-16
Your first step will be read the documentation for HDL Coder and walk through the tutorial examples provided. This will introduce you to the tool and teach you its basic functionality, capabilities, limitations, and requirements. Then, if you have a specific question that you are stuck on, you can ask a question that can be answered with more than a generality.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!