close all; clear all; %-------------------- % Generates baseband signal for the COM-8001 pattern generator % Format is 2 (complex) * 10-bit (unsigned). %-------------------- fs = 64; nsamples = 64; % simulation duration, expressed as number of complex samples n_b = 20; % Number of Data Bits gain = .3; % prevent saturation at the D/A converter % for long simulations, store results in output file [fid_o1,message] = fopen('pn32spread429a.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 x(t) %Collected data from sensors length of 19 xt_ip = 2*(rand(1,(n_b-1))>.5) - 1; %xt_ip = rand(1,n_b)>.5; %add 1 to begining of xt to make sequence length of 20 %this is for when receiving, you always know there is a 1 %in the beginning xt_ipnew = [1 ; xt_ip.']; xt_ipnew = xt_ipnew.'; xt = kron(xt_ipnew, ones(1,nsamples)); newxt = gain*xt; %------------------------------------------------------ % MAIN LOOP %------------------------------------------------------ % loop signal processing %adding sequence for time sync ts = .5*[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]; %PN code pn = [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]; k=1; for i = 1:length(pn) pnf(k:k+1) = pn(i); k=k+2; end pnnew = kron(ones(1,n_b), pnf); pn_new = gain*pnnew; %mix pn code with data sequence new_xt = xt.*pn_new; %design cosine filter yf = rcosine(1,8,'fir'); %upsample and filter yo = rcosflt(new_xt, 1, 8, 'filter', yf); tsf = rcosflt(ts, 1, 8, 'filter',yf); %data needed for trasmitting newyo = yo(25:5168-24); %newyo= yo; %make sure to copy this matrix to receiver code newts = tsf(25:304-24); %newts = tsf; %this is final signal with time sync sequence attached to front signali = [tsf ; yo]; signalq = [tsf ; yo]; for j = 1:length(signali) i = j; % convert samples to 10-bit unsigned format sigt2i = min(max((floor((signali(i)+1.)*511.5)),0.),1023); sigt2q = min(max((floor((signalq(i)+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 figure(1) %subplot(4,1,1) plot(xt) grid on; xlabel('Time'); ylabel('Amplitude'); legend('Data Stream - x(t)'); figure(2) %subplot(4,1,2) plot(new_xt); grid on; xlabel('Time'); ylabel('Amplitude'); legend('Data Stream Mixed With PN Code'); figure(3) %subplot(4,1,3) plot(signali, 'b'); grid on; xlabel('Time'); ylabel('Amplitude'); legend('Filtered I Values'); figure(4) %subplot(4,1,4) plot(signalq, 'r'); grid on; xlabel('Time'); ylabel('Amplitude'); legend('Filtered Q Values'); % close opened files fclose(fid_o1);