close all; clear all; % MATLAB program to sort data captured using % the Com-8002 and the Com-3001 modules into % I and Q samples.The file must have been saved % in binary format (decimal files are % NOT supported in this code). n_b = 20; % Replace 'file_name_including_full_path' % with the full path and file name for the % file where the binary data is stored. % For example: % fid = fopen('c:\My Documents\Captured_Data.txt','r'); fid = fopen('test2','r'); % Read the content of the file into an array. data = fread(fid,inf,'uchar') % Find out how many samples are in the array. [dataHeight garbage] = size(data); % Find out how big the matrix holding the % 20-bit wide data needs to be. binArrayHeight = ceil(dataHeight/2.5); % Pre-initialize the matrix holding 20-bit data % for faster program execution. binArray = zeros(binArrayHeight,20); % Initialize the index counter. rowCounter = 1; % Start the conversion from 8 bits to 20 bits. % Note: the de2bi function places the MSb % on the rightmost position. Therefore, % use it with fliplr to get the MSb in the % leftmost position. for i = 1:2:binArrayHeight % Store I sample binArray(i,1:8) = fliplr(de2bi(data(rowCounter),8)); % Store bits 19-12 rowCounter = rowCounter + 1; binArray(i,9:16) = fliplr(de2bi(data(rowCounter),8)); % Store bits 11-4 rowCounter = rowCounter + 1; binaryWord = fliplr(de2bi(data(rowCounter),8)); % A temporary array binArray(i,17:20) = binaryWord(1:4); % Store bits 3-0 % Store Q sample binArray(i+1,1:4) = binaryWord(5:8); % Store bits 19-16 rowCounter = rowCounter + 1; binArray(i+1,5:12) = fliplr(de2bi(data(rowCounter),8)); % Store bits 15-8 rowCounter = rowCounter + 1; binArray(i+1,13:20) = fliplr(de2bi(data(rowCounter),8)); % Store bits 7-0 rowCounter = rowCounter + 1; end % Compensate for the fact that the pinouts are % reversed (MSb on Com 8002 input connector % corresponds to LSb on Com 3001 output connector). binArray(:, 1:10) = binArray(:, 10:-1:1); % Reverse I binArray(:, 11:20) = binArray(:, 20:-1:11); % Reverse Q % Pre-initialize the arrays for faster program execution. iSamples = zeros(binArrayHeight, 1); qSamples = zeros(binArrayHeight, 1); % Convert the 10-bit binary numbers into decimal numbers % in the range 0-1023. for i = 1:binArrayHeight iSamples(i,1) = bi2de(fliplr(binArray(i, 1:10))); qSamples(i,1) = bi2de(fliplr(binArray(i, 11:20))); end %place time sync sequence from transmitter here ts = [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]; %cosinefilter pn yf = rcosine(1,8,'fir'); tsf = rcosflt(ts, 1, 8, 'filter',yf); newts = tsf(25:304-24); %newts = tsf; corr_peak=conv((iSamples+sqrt(-1)*qSamples),conj(newts(length(newts):-1:1))); peak=find((abs(corr_peak)>max(abs(corr_peak))-10000)); peaknew = peak < 20992; peak_new = peaknew.*peak; start=peak_new+9; newi = iSamples(start: start+10250-1); newq = qSamples(start: start+10250-1); %downsample to bring back to original length dsiSamp = newi(1:8:length(newi)); dsqSamp = newq(1:8:length(newq)); %shift received signal down to x-axis for i = 1:length(dsiSamp) downishift(i) = dsiSamp(i)-500; downqshift(i) = dsqSamp(i)-500; end %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 newpnf = .3*pnf; %use PN code to get back to original signal o = 1; for i = 1:20 unpni(o:o+63) = downishift(o:o+63).*newpnf; unpnq(o:o+63) = downqshift(o:o+63).*newpnf; o = o+64; end %constellation code %shift back up to original position %shift received signal down to x-axis for i = 1:(length(dsiSamp)-2) upishift(i) = unpni(i)+500; upqshift(i) = unpnq(i)+500; end %taking area under these 2 samples of the %curve per bit when shifted and not shifted k = 1; for i = 1:20 %totali(i) = sum(unpni(k+11:k+12)); totali(i) = sum(unpnq(k+10:k+11)); newtotali(i) = sum(dsiSamp(k:k+63)); newtotalq(i) = sum(dsqSamp(k:k+63)); k=k+64; end %average under the whole received signal %avi = sum(totali)/20; %avq = sum(totalq)/20; %if the area of the first quarter of the bit %(when shifted) is less than 0 then it is a -1 else 1 for i = 1:20 if totali(i) > 0 consti(i) = -newtotali(i); constq(i) = -newtotalq(i); else consti(i) = newtotali(i); constq(i) = newtotalq(i); end %if totalq(i) > 0 % constq(i) = -newtotalq(i); %else % constq(i) = newtotalq(i); %end end %extracting data sequence g = 1; for i = 1:20 if consti(i) > 0 extractedxt(g:g+63) = 1; else extractedxt(g:g+63) = -1; end g=g+64; end figure(1) subplot(2,1,1) plot(iSamples); grid on; xlabel('Time'); ylabel('Amplitude'); legend('Received I Samples'); subplot(2,1,2) plot(qSamples,'r'); grid on; xlabel('Time'); ylabel('Amplitude'); legend('Received Q Samples'); figure(2) plot(abs(corr_peak)); grid on; xlabel('Time'); ylabel('Amplitude'); legend('Time Synchronization'); %figure(3) %subplot(2,1,1) %plot(dsiSamp); %grid on; %xlabel('Time'); %ylabel('Amplitude'); %legend('Downsampled I Samples After Synchronization'); %subplot(2,1,2) %plot(dsqSamp,'r'); %grid on; %xlabel('Time'); %ylabel('Amplitude'); %legend('Downsampled Q Samples After Synchronization'); %figure(4) %subplot (2,1,1) %plot(upishift); %grid on; %xlabel('Time'); %ylabel('Amplitude'); %legend('Extracted I Signal Using PN Code'); %subplot(2,1,2) %plot(upqshift, 'r') %grid on; %xlabel('Time'); %ylabel('Amplitude'); %legend('Extracted Q Signal Using PN Code'); figure(5) plot(consti,constq,'b.'); xlabel('Real - I'); ylabel('Imaginary - Q'); legend('Constellation Points'); %figure(6) %plot(extractedxt); %grid on; %xlabel('Time'); %ylabel('Amplitude'); %legend('Extracted Data Stream');