function slide=basicsimdemo
% Basic communications link simulation demonstration
%
%   BASICSIMDEMO is a slideshow demonstration of phase shift keying in noise. It
%   describes the components necessary to create a simulation of modulation and
%   demodulation in the presence of additive white gaussian noise.  It
%   demonstrates how existing functions, DMOD and DDEMOD, enhanced functions,
%   SYMERR and BITERR, and new functions RANDSRC, and AWGN can be used together
%   to simulate a communications link.  Finally, it generates two sets of
%   performance curves for QPSK using two example functions.  The first,
%   SIMPASSBANDEX, simulates in passband mode while the second, SIMBASEBANDEX,
%   simulates in baseband mode.

% Copyright 1996-2000 The MathWorks, Inc.
% $Revision: 1.4 $  $Date: 2000/08/31 15:20:18 $
if nargout<1,
  playshow basicsimdemo
else
  %========== Slide 1 ==========

  slide(1).code={
    'cla;',
    'fig = gcf;',
    'sp = get(fig,''position'');',
    'ss = get(0,''ScreenSize'');',
    'fp1 = [ss(3)*0.5-sp(3)*0.55  ss(4)*.80-sp(4) sp(3)*1.1 sp(4)*1.2];',
    'if ichange == 0  & size(slideData.param,2) == 1; set(gcf,''position'',fp1); end;',
    'hClose  =    findobj(fig, ''Tag'', ''close'');',
    'stClose = get(hClose,                 ''enable'');',
    'stAuto  = get(slideData.autohandle,   ''enable'');',
    'stReset = get(slideData.resethandle,  ''enable'');',
    'stPrev  = get(slideData.backhandle,   ''enable'');',
    'stNext  = get(slideData.nexthandle,   ''enable'');',
    'set(hClose,                 ''enable'', ''off'');',
    'set(slideData.autohandle,   ''enable'', ''off'');',
    'set(slideData.resethandle,  ''enable'', ''off'');',
    'set(slideData.backhandle,   ''enable'', ''off'');',
    'set(slideData.nexthandle,   ''enable'', ''off'');',
 	'SNRspBIT = [1:9]; k = 2;',
    'expBER = 0.5.*erfc(sqrt(10.^(SNRspBIT(:).*0.1)) );',
    'expSER = 1 - (1 - expBER) .^ k;',
 	'semilogy(SNRspBIT, expBER,''m-'',SNRspBIT, expSER,''g-'');grid on;',
	'title(''Performance of QPSK'');'
    'xlabel(''SNR/bit (dB)'');',
    'ylabel(''SER and BER'');',
    'legend(''BER'',''SER'',0);',
    'set(hClose,                 ''enable'', stClose );',
    'set(slideData.autohandle,   ''enable'', stAuto  );',
    'set(slideData.resethandle,  ''enable'', stReset );',
    'set(slideData.backhandle,   ''enable'', stPrev  );',
    'set(slideData.nexthandle,   ''enable'', stNext  );',
    'set(slideData.resethandle,''UserData'',SlideShowi);',
	'Fd = 1; Fc = 4; Fs = 32; N = Fs/Fd; numSymb = 100;     ',
	'M = 4; SNRpBit = 14; SNR = SNRpBit/log2(M);                     ',
	'seed = [12345 54321];              ',
	'rand(''state'', seed(1));  randn(''state'', seed(2));              ',
	};
  slide(1).text={
    'This demo shows how to simulate a basic quarternary phase shift keying (QPSK) communication link that will allow you to generate empirical performance curves that can be compared to those above. ',
    '',
    'The first step is to initialize variables for data rate, carrier frequency, sampling rates, number of symbols to simulate, alphabet size (M), signal to noise ratios (SNR, SNR/bit). The last line seeds the random number generators.',
   '',
	'Fd = 1; Fc = 4; Fs = 32; N = Fs/Fd; numSymb = 1000;     ',
	'M = 4; SNRpBit = 14; SNR = SNRpBit/log2(M);                     ',
	'rand(''state'', 12345);  randn(''state'', 54321);              '
   };

  %========== Slide 2 ==========

  slide(2).code={
    'cla;',
	'numPlot = 10;                      ',
	'rand(''state'', seed(1));              '
	'msg_orig = randsrc(numSymb,1,[0:M-1]);                      ',
	'stem([0:numPlot-1], msg_orig(1:numPlot),''bx'');                      '
	'axis([ 0 numPlot -0.2 3.2]);  xlabel(''Time''); ylabel(''Amplitude''); % Set axes and labels.'
	};
  slide(2).text={
    'Next, use RANDSRC to generate random information symbols from 0 to M-1.  Since the simulation is of QPSK, the symbols are 0 through  3. The first 10 data points are plotted above.',
   '',
	'numPlot = 10;                      ',
	'msg_orig = randsrc(numSymb,1,[0:M-1]);                      ',
	'stem([0:numPlot-1], msg_orig(1:numPlot),''bx'');                      ',
	'axis([ 0 numPlot -0.2 3.2]);  xlabel(''Time''); ylabel(''Amplitude''); '
   };

  %========== Slide 3 ==========

  slide(3).code={
	'grayencod = bitxor([0:M-1],floor([0:M-1]/2)); msg_gr_orig = grayencod(msg_orig+1);',
	'msg_tx  = dmod(msg_gr_orig, Fc, Fd, Fs, ''psk'', M);',
	'numModPlot = numPlot * Fs; ',
	't = [0:numModPlot-1]./Fs;          ',
	'plot(t, msg_tx(1:length(t)),''b-'');                      ',
	'axis([ min(t) max(t) -1.5 1.5]);  xlabel(''Time''); ylabel(''Amplitude''); '
	};
  slide(3).text={
    'Use DMOD to phase modulate the data on a carrier with frequency 4 times the data rate and to upsample to a sampling rate 8 times the carrier frequency. Note in the above plot that the phase changes at the symbol boundaries.',
   '',
	'grayencod = bitxor([0:M-1],floor([0:M-1]/2)); msg_gr_orig = grayencod(msg_orig+1);',
	'msg_tx  = dmod(msg_gr_orig, Fc, Fd, Fs, ''psk'', M);',
	'numModPlot = numPlot * Fs; t = [0:numModPlot-1]./Fs;          ',
	'plot(t, msg_tx(1:length(t)),''b-'');                      ',
	'axis([ min(t) max(t) -1.5 1.5]);  xlabel(''Time''); ylabel(''Amplitude'');'
   };

  %========== Slide 4 ==========

  slide(4).code={
	'randn(''state'', seed(2));              '
	'msg_rx  = awgn(msg_tx, SNR-10*log10(0.5)-10*log10(N), ''measured'', [], ''dB'');',
	'plot(t, msg_rx(1:length(t)),''b-'');                      ',
	'axis([ min(t) max(t) -1.5 1.5]);  xlabel(''Time''); ylabel(''Amplitude''); '
	};
  slide(4).text={
    'Then use AWGN to add noise to the transmitted signal to create the noisy signal at the receiver.  Use the ''measured'' option to add noise that is 14 dB below the average signal power (SNR = 14 dB).  The term, -10*log10(N), is used to scale the noise power with the oversampling.  The term, -10*log10(0.5), is used to reduce the noise power to match the bandpass signal representation.  The received signal is plotted above.',
   '',
	'msg_rx  = awgn(msg_tx, SNR-10*log10(0.5)-10*log10(N), ''measured'', [], ''dB'');',
	'plot(t, msg_rx(1:length(t)),''b-'');                      ',
	'axis([ min(t) max(t) -1.5 1.5]);  xlabel(''Time''); ylabel(''Amplitude'');'
   };

  %========== Slide 5 ==========

  slide(5).code={
	'msg_gr_demod = ddemod(msg_rx, Fc, Fd, Fs, ''psk'', M);',
	'[dummy graydecod] = sort(grayencod); graydecod = graydecod - 1;',
	'msg_demod = graydecod(msg_gr_demod+1)'';',
	'stem([0:numPlot-1], msg_orig(1:numPlot),''bx''); hold on;  '
	'stem([0:numPlot-1], msg_demod(1:numPlot),''ro''); hold off; '
	'axis([ 0 numPlot -0.2 3.2]);  xlabel(''Time''); ylabel(''Amplitude'');'
	};
  slide(5).text={
    'Then use DDEMOD to demodulate the signal, detect the symbol that was transmitted, and downsample to the original information rate.  The detected symbols are plotted in red stems with circles and the transmitted symbols are plotted in blue stems with x''s.  The blue stems of the transmitted signal are shadowed by the red stems of the received signal.  Therefore, comparing  the blue x''s with the red circles indicates that the received signal is identical to the transmitted signal. ',
   '',
	'msg_gr_demod = ddemod(msg_rx, Fc, Fd, Fs, ''psk'', M);',
	'[dummy graydecod] = sort(grayencod); graydecod = graydecod - 1;',
	'msg_demod = graydecod(msg_gr_demod+1)'';',
	'stem([0:numPlot-1], msg_orig(1:numPlot),''bx''); hold on;  '
	'stem([0:numPlot-1], msg_demod(1:numPlot),''ro''); hold off; '
	'axis([ 0 numPlot -0.2 3.2]);  xlabel(''Time''); ylabel(''Amplitude'');'
   };

  %========== Slide 7 ==========

  slide(6).code={
	'[errorBit ratioBit] = biterr(msg_orig, msg_demod, log2(M));',
	'[errorSym ratioSym] = symerr(msg_orig, msg_demod);        '
	};
  slide(6).text={
    'Finally, use BITERR and SYMERR to compare the original message to the demodulated message.  BITERR is used to determine the bit error rate and SYMERR is used to determine the symbol error rate. ',
    '',
	'[errorBit ratioBit] = biterr(msg_orig, msg_demod, log2(M));',
	'[errorSym ratioSym] = symerr(msg_orig, msg_demod);        ',
    '',
    'The next two slides execute examples files, SIMPASSBANDEX and SIMBASEBANDEX, which are complete passband and baseband simulation examples for Quaternary PSK (QPSK), respectively.  They demonstrate how to create simulation drivers in MATLAB that plot the simulation results as they are generated.'
   };

  %========== Slide 7 ==========

  slide(7).code={
    'cla;',
    'hClose  =    findobj(fig, ''Tag'', ''close'');',
    'stClose = get(hClose,                 ''enable'');',
    'stAuto  = get(slideData.autohandle,   ''enable'');',
    'stReset = get(slideData.resethandle,  ''enable'');',
    'stPrev  = get(slideData.backhandle,   ''enable'');',
    'stNext  = get(slideData.nexthandle,   ''enable'');',
    'set(hClose,                 ''enable'', ''off'');',
    'set(slideData.autohandle,   ''enable'', ''off'');',
    'set(slideData.resethandle,  ''enable'', ''off'');',
    'set(slideData.backhandle,   ''enable'', ''off'');',
    'set(slideData.nexthandle,   ''enable'', ''off'');',
	'title(''Performance of Passband QPSK'');'
 	'clear simpassbandex; simpassbandex([0:1:5]);'
    'set(hClose,                 ''enable'', stClose );',
    'set(slideData.autohandle,   ''enable'', stAuto  );',
    'set(slideData.resethandle,  ''enable'', stReset );',
    'set(slideData.backhandle,   ''enable'', stPrev  );',
    'set(slideData.nexthandle,   ''enable'', stNext  );',
    'set(slideData.resethandle,''UserData'',SlideShowi);',
	};
  slide(7).text={
    'Please wait a moment for the first simulation point to be generated.',
    '',
    'The green and magenta lines are the theoretical bit error rate (BER) and symbol error rate (SER) performance curves for QPSK, respectively.  The example, SIMPASSBANDEX, plots the simulated BER and SER in red and blue lines, respectively. SIMPASSBANDEX uses DMOD and DDEMOD to simulate QPSK at passband.',
    '',
	'simpassbandex([0:1:5])        ',
   };
  %========== Slide 8 ==========

  slide(8).code={
    'cla;',
    'hClose  =    findobj(fig, ''Tag'', ''close'');',
    'stClose = get(hClose,                 ''enable'');',
    'stAuto  = get(slideData.autohandle,   ''enable'');',
    'stReset = get(slideData.resethandle,  ''enable'');',
    'stPrev  = get(slideData.backhandle,   ''enable'');',
    'stNext  = get(slideData.nexthandle,   ''enable'');',
    'set(hClose,                 ''enable'', ''off'');',
    'set(slideData.autohandle,   ''enable'', ''off'');',
    'set(slideData.resethandle,  ''enable'', ''off'');',
    'set(slideData.backhandle,   ''enable'', ''off'');',
    'set(slideData.nexthandle,   ''enable'', ''off'');',
	'title(''Performance of Baseband QPSK'');'
 	'clear simbasebandex; simbasebandex([0:1:5]);'
    'set(hClose,                 ''enable'', stClose );',
    'set(slideData.autohandle,   ''enable'', stAuto  );',
    'set(slideData.resethandle,  ''enable'', stReset );',
    'set(slideData.backhandle,   ''enable'', stPrev  );',
    'set(slideData.nexthandle,   ''enable'', stNext  );',
    'set(slideData.resethandle,''UserData'',SlideShowi);',
	};
  slide(8).text={
    'Please wait a moment for the first simulation point to be generated.',
    '',
    'The green and magenta lines are the theoretical bit error rate (BER) and symbol error rate (SER) performance curves for QPSK, respectively.  The example, SIMBASEBANDEX, plots the simulated BER and SER in red and blue lines, respectively. SIMBASEBANDEX uses DMODCE and DDEMODCE to simulate PSK at baseband using a complex envelope representation of the modulated signal.  Using DMODCE and DDEMODCE instead of DMOD and DDEMOD allows you to simulate communication links using a lower sampling rate, reducing the simulation time. ',
    '',
	'simbasebandex([0:1:5]);'
   };
end
