clear all; freq = 1000e3; %freq of carrier is 1Mhz P = 1/sqrt(4); N = 32; n_b = 10; n_fft = 32*n_b; time = 0: (1/(N*freq)): (n_b/freq); time = time(1:length(time)-1); % for long simulations, store results in output file [fid_o1,message] = fopen('bpskmod.bin','w'); if(fid_o1 == -1) message end % initialization last_i = 0; last_q = 0; current_i = 0; current_q = 0; b = dec2bin(0,10); %data that will be transmitted, xt %xt_ip = 2*(rand(1,n_b) > .5) - 1 %xt_ip = ones(1,n_b); %xt = kron(xt_ip, ones(1,N)) %PN code gt = [1 -1 1 1 -1 -1 1 -1 1 -1 -1 1 1 1 -1 1 1 1 -1 -1 1 -1 1 -1 1 1 1 -1 1 -1 1 -1]; gt_new = kron(ones(1,n_b), gt); %------------------------------------------------------ % MAIN LOOP %------------------------------------------------------ % loop signal processing (cannot use vectors) for j = 1:length(time) i = j; xt(i) = 1; %gt_new(i) = 1; %carrier freq, ct cti = sqrt(2*P)*cos(2*pi*freq*time(i)); ctq = sqrt(2*P)*sin(2*pi*freq*time(i)); %BPSK mod data, sxt sxtiout = cti*xt(i); sxtqout = ctq*xt(i); test1(i) = sxtiout; test2(i) = sxtqout; %BPSK code mod data, st sti = sxtiout .* gt_new(i); stq = sxtqout .* gt_new(i); test3(i) = sti; test4(i) = stq; % convert samples to 10-bit unsigned format sigt2i = min(max((floor((sti+1.)*511.5)),0.),1023); sigt2q = min(max((floor((stq+1.)*511.5)),0.),1023); %invert bit order because of pinout a = dec2bin(sigt2i, 10); b(1) = a(10); b(2) = a(9); b(3) = a(8); b(4) = a(7); b(5) = a(6); b(6) = a(5); b(7) = a(4); b(8) = a(3); b(9) = a(2); b(10) = a(1); outputi = bin2dec(b); a = dec2bin(sigt2q, 10); b(1) = a(10); b(2) = a(9); b(3) = a(8); b(4) = a(7); b(5) = a(6); b(6) = a(5); b(7) = a(4); b(8) = a(3); b(9) = a(2); b(10) = a(1); outputq = bin2dec(b); % pack samples % wait until two complex samples are collected before writing into file if(mod(j,2) == 0) % even sample current_i = outputi; current_q = outputq; a = bitand(bitshift(last_q,-2), 255); fprintf(fid_o1,'%c',a); a = bitand(bitor(bitshift(last_q,6),bitshift(last_i,-4)),255); fprintf(fid_o1,'%c',a); a = bitand(bitor(bitshift(last_i,4),bitshift(current_q,-6)),255); fprintf(fid_o1,'%c',a); a = bitand(bitor(bitshift(current_q,2),bitshift(current_i,-8)),255); fprintf(fid_o1,'%c',a); a = bitand(current_i,255); fprintf(fid_o1,'%c',a); % keep track of simulation progress if(mod(j,10000) == 0) j/10000 end; else % odd sample % remember last sample last_i = outputi; last_q = outputq; end; end %plot(sxtiout); %hold on; %plot(sxtqout, 'r'); subplot(5,1,1) plot(time,xt,'b'); axis([0 time(length(time)) -1.5 1.5]); grid on; legend('x(t) - binary data'); %xlabel('time (us)'); %ylabel('Amplitude') title('BPSK DSSS Transmitter - Time Domain Waveforms'); subplot(5,1,2) plot(time,test1); axis([0 time(length(time)) -1.5 1.5]); grid on; legend('sx(t) - BPSK i waveform'); %xlabel('time (us)'); %ylabel('Amplitude') %title('sxti') subplot(5,1,3) plot(time,test2); axis([0 time(length(time)) -1.5 1.5]); grid on; legend('sx(t) - BPSK q Waveform'); %xlabel('time (us)'); ylabel('Amplitude') %title('sxtq') subplot(5,1,4) plot(time,test3); axis([0 time(length(time)) -1.5 1.5]); grid on; legend('s(t) - BPSK DSSS i Waveform'); %xlabel('time (us)'); %ylabel('Amplitude') %title('sxti') subplot(5,1,5) plot(time,test4); axis([0 time(length(time)) -1.5 1.5]); grid on; legend('s(t) - BPSK DSSS q Waveform'); %xlabel('time (us)'); ylabel('Amplitude') title('sxtq') % close opened files fclose(fid_o1);