%-- SEND.M: a complex-PAM transmitter --------------------------------------% clear; %-- System parameters ------------------------------------------------------% M = 16; % size of alphabet Ltrain = 1000; % number of training symbols to transmit f0 = 2000; % carrier frequency in Hz q = 20; % q = number of samples per baud; baud rate = 8192/q. timelag = 20; % delay in symbol periods of transmit pulse g(t) Rsample = 8192; % computer sample period (in secs; can't be changed) filename = 'Pics/eye.jpg'; % name of JPEG image to be transmitted %-- Define alphabet --------------------------------------------------------% %---------------------------------------------------------------------------% sM = sqrt(M); [x,y] = meshgrid((-sM+1):2:(sM-1),(-sM+1):2:(sM-1)); alphabet = x(:) + 1i*y(:); %M-QAM alphabet; sqrt(M) must be a power of 2 %-- Convert image to bits & scramble ---------------------------------------% %---------------------------------------------------------------------------% X = double(imread(filename)); clf; subplot(222); imshow(uint8(X)); clear bits; for k=1:8, bits(:,k)=bitget(X(:),k); end; bits = [bits(:); zeros(log2(M) - rem(length(bits(:)),log2(M)),1)]; rand('state',0); scrambler = rand(length(bits),1)>0.5; bits = xor(bits, scrambler); clear scrambler; %-- Convert bits to symbols ------------------------------------------------% %---------------------------------------------------------------------------% bits = reshape(bits,length(bits)/log2(M),log2(M)); a = alphabet(1 + bits*2.^(0:(log2(M)-1)).'); L = length(a); %---------------------------------------------------------------------------% disp(' '); disp([' At a baud rate of 8192/',num2str(q),' = ',num2str(8192/q), ... ' baud, it will take ']); disp([' ', num2str((L + Ltrain)*q/Rsample),' secs to send ', ... num2str(Ltrain),' training and ',num2str(L),' data symbols.']) disp([' With ', num2str(M),'-ary complex PAM, the bit rate will be ', ... num2str(log2(M)*8192/q),' b/s.']); disp(' '); %-- Generate QAM signal -------------------------------------------% rand('state',0); train = alphabet(ceil(M*rand(Ltrain,1))); chips = [[train.' a.']; zeros(q-1,Ltrain+L)]; % insert q-1 0's btwn ea symbl tbyT = (-timelag*q : timelag*q) / q; g = sinc(tbyT); s_tilde = conv(g, chips(:)); s = real(s_tilde .* exp(1i*2*pi*f0*(1:length(s_tilde)).'/Rsample)); s = s/max(abs(s)); clear a s_tilde chips train; %-- Transmit signal --------------------------------------------------------% %---------------------------------------------------------------------------% reply = [input('Transmit through speaker (y/n)? [y] : ','s'),' ']; if reply(1) ~= 'n', disp('Execute the following command at the receiver:'); disp(' '); disp(' audiorecord > Share/recorded.au'); disp(' '); input('Then hit return to begin transmission ... ','s'); sound(s) disp('Done. Hit at receiver to stop recording.'); end; %-- Save information to be Shared with receiver ----------------------------% %---------------------------------------------------------------------------% disp('Saving data ...'); parms = [Rsample/q; f0; Ltrain; L; timelag; size(X).']; save Share/parms parms -ascii save Share/alphabet alphabet save Cheating/bits bits save Cheating/X X save Cheating/s s -ascii clear