close all; clear all; % file: com8001r2.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). % Generate two sinewaves, at frequency fc1 and fc2. %-------------------- fs = 200000; % sampling rate 200 KHz typ. nsamples = 200; % simulation duration, expressed as number of complex samples fc1 = 1000; % sinewave1 in Hz fc2 = -2000; % sinewave2 in Hz gain1 = 0.7 % amplitude scaling factor gain2 = 0.7 % amplitude scaling factor % for long simulations, store results in output file [fid_o1,message] = fopen('com8001r2.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 % simulate the first sinewave phase1 = 2.*pi*fc1*(i-1)/fs; % carrier phase carrier1_cos = cos(phase1); carrier1_sin = sin(phase1); sigt1i = carrier1_cos*gain1; sigt1q = carrier1_sin*gain1; test1(i) = sigt1i; test2(i) = sigt1q; % simulate the second sinewave phase2 = 2.*pi*fc2*(i-1)/fs; % carrier phase carrier2_cos = cos(phase2); carrier2_sin = sin(phase2); sigt2i = carrier2_cos*gain2; sigt2q = carrier2_sin*gain2; test3(i) = sigt2i; test4(i) = sigt2q; % sum both signals. Scale to prevent overflow sigt3i = (sigt1i + sigt2i)/2.; sigt3q = (sigt1q + sigt2q)/2.; test5(i) = sigt3i; test6(i) = sigt3q; % convert samples to 10-bit unsigned format sigt3i = min(max((floor((sigt3i+1.)*511.5)),0.),1023); sigt3q = min(max((floor((sigt3q+1.)*511.5)),0.),1023); %invert bit order because of pinout a = dec2bin(sigt3i, 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(sigt3q, 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 into byte-size format % wait until two complex samples are collected before writing into file (makes it easier to % manage the byte alignments): 4 * 10-bits is a multiple of 8. 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; subplot(3,1,1); title('First Sinewave'); plot(test1); hold on; plot(test2,'r'); ylabel('amplitude') subplot(3,1,2); title('Second Sinewave'); plot(test3); hold on; plot(test4,'r'); ylabel('amplitude') subplot(3,1,3); title('Scaled Sum of Both Sinewaves'); plot(test5); hold on; plot(test6,'r'); xlabel('samples'); ylabel('amplitude') end % close opened files fclose(fid_o1);