matlab使用demos,关于“Matlab Demos”的学习和总结

这里总结了Matlab Demos下通信工具箱里面的examples,给出源代码和注释。

程序代码 (一):关于BPSK的Mente Carlo仿真

% -------------------------------------------------------------------------

% Matlab Demos                                                      No.1.1

% -------------------------------------------------------------------------

% Phase Shift Keying Simulation

% This demo shows how to simulate a basic Quarternary Phase Shift Keying

% (QPSK) communication link, and to generate empirical performance curves

% that can be compared to theoretical predictions.

% -------------------------------------------------------------------------

% 1. 两个函数的使用:rectpulse与intdump成对使用,重复插值与抽取,互为逆操作;

% 2. randn的seed的使用;

% 3. 这里的解调是直接采用MATLAB自带的pskmod、pskdemod函数;

% 4. 为什么在后面调用函数,当采用的EbNo越大的时候,计算需要的时间越长?甚至是

%    系统死机(大概超过10dB之后)?

% 5. 这里需要讨论的问题就是 Mente Carlo 仿真的数值问题,即当用一个试验样本函数去

%    估计一个观测值的时候,如何尽量减小估计值的方差问题(选择合适的仿真点总数和期望错误点数)。

% -------------------------------------------------------------------------

%                                       revised by lavabin  2006.07.26

% -------------------------------------------------------------------------

clc;clear all;close all;echo off;tic;

% -------------------------------------------------------------------------

%                      Parameter Definition

% -------------------------------------------------------------------------

nSamp = 8; numSymb = 100;

M = 4; SNR = 14;

seed = [12345 54321];

rand('state', seed(1)); randn('state', seed(2));

% -------------------------------------------------------------------------

%                Generating random information symbols

% -------------------------------------------------------------------------

numPlot = 10;

rand('state', seed(1));

msg_orig = randsrc(numSymb, 1, 0:M-1);

% -------------------------------------------------------------------------

% Phase modulating the data

% Use PSKMOD to phase modulate the data and RECTPULSE to upsample to a

% sampling rate 8 times the carrier frequency.

% -------------------------------------------------------------------------

grayencod = bitxor(0:M-1, floor((0:M-1)/2));

% Gray coding here!

% %  a = bitxor(0:M-1, floor((0:M-1)/2))

% %  a = 0     1     3     2

msg_gr_orig = grayencod(msg_orig+1);

% % Using "looktable" to map source data to Gray code

msg_tx = pskmod(msg_gr_orig,M);

% % Mapping source data to MPSK constellation

msg_tx = rectpulse(msg_tx,nSamp);

% -----------------------------

% figure(1);

% -----------------------------

scatterplot(msg_tx);

hold on;grid on;

% -------------------------------------------------------------------------

% Creating the noisy signal

% 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). Plot the

% constellation of the received signal.

% -------------------------------------------------------------------------

randn('state', seed(2));

msg_rx = awgn(msg_tx, SNR, 'measured', [], 'dB');

% -----------------------------

% figure(2);

% -----------------------------

scatterplot(msg_rx);

hold on;grid on;

% -------------------------------------------------------------------------

% Recovering information from the transmitted signal

% Use INTDUMP to downsample to the original information rate. Then use

% PSKDEMOD to demodulate the signal, and detect the transmitted symbols.

% 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_rx_down  = intdump(msg_rx,nSamp);  % operation before DEMOD

msg_gr_demod = pskdemod(msg_rx_down,M);

[dummy graydecod] = sort(grayencod);

graydecod = graydecod - 1;

msg_demod = graydecod(msg_gr_demod+1)';

figure(3);

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');

% -------------------------------------------------------------------------

% Comparing original message to demodulated message

% -------------------------------------------------------------------------

[errorBit ratioBit] = biterr(msg_orig, msg_demod, log2(M));

[errorSym ratioSym] = symerr(msg_orig, msg_demod);

% -------------------------------------------------------------------------

% Running simulation examples

% The next step executes an example file SIMBASEBANDEX, which is a complete

% sin of the modulated

% signal.mulation example for QPSK. It demonstrates how to create simulation

% drivers in MATLAB that plot the simulation results as they are generated.

% -------------------------------------------------------------------------

% Running the QPSK simulation example

% 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 PSKMOD and PSKDEMOD to simulate

% PSK at baseband using a complex envelope representation.

% -------------------------------------------------------------------------

% [MPSK_ratio, MPSK_errors] = simbasebandex_lavabin(0:2:10)

[MPSK_ratio, MPSK_errors] = simbasebandex_lavabin(0:.5:10)

% Note:这个函数simbasebandex_lavabin在整个script中只被调用了一次。

simulation_time = toc

% -------------------------------------------------------------------------

%                              End of Script

% -------------------------------------------------------------------------

displayEndOfDemoMessage(mfilename);

% ---------------------------------ÿ

你可能感兴趣的:(matlab使用demos)