close all; clear all; % file: com8001r1.M % modified: 8-29-02 % Author: MSS/Alain Zarembowitch % %-------------------- % Generates baseband signal for the COM-8001 pattern generator % Format is 2 (complex) * 10-bit (unsigned). % Signal is a sinewave at frequency fc. %-------------------- fs = 200000; % sampling rate 200 KHz typ. nsamples = 200; % simulation duration, expressed as number of complex samples fc = 1000; % sinewave frequency in Hz gain = 0.7 % prevent saturation at the D/A converter % for long simulations, store results in output file [fid_o1,message] = fopen('com8001r1.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); %------------------------------------------------------ % MAIN LOOP %------------------------------------------------------ % loop signal processing (cannot use vectors) for j = 1:nsamples i = j phase = 2.*pi*fc*(i-1)/fs; % carrier phase carrier_cos = cos(phase); carrier_sin = sin(phase); sigt1i = carrier_cos*gain; sigt1q = carrier_sin*gain; % convert samples to 10-bit unsigned format % (numbers are pretty much the same after this calculation) sigt2i = min(max((floor((sigt1i+1.)*511.5)),0.),1023); sigt2q = min(max((floor((sigt1q+1.)*511.5)),0.),1023); test1(i) = sigt1i; test2(i) = sigt1q; %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); test5(i) = outputi; 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); test6(i) = outputq; % 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 of the sin (i and q values) plot(test1); title('I (blue) and Q (red) values for a Baseband signal composed of a Sinewave at 1MHz'); hold on; plot(test2,'r'); xlabel('samples'); ylabel('amplitude'); % close opened files fclose(fid_o1);