|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 House 于 2020-4-21 14:03 编辑
! y( g% {* ]& K' j% N5 C% E- u9 V' @3 f% w% x9 R
OFDM的仿真程序,并且包括详细的WORD说明。有误码率和各种性能分析的做图程序。有需要的可以看看。: l; n6 ~$ D O4 x# u
; R# N; c; [5 O% E, {* c6 N/ v%a_run_design.m文件
* b& z/ r1 ~# z: Nsetup %系统设置/ ^( _! {$ ^" f# l) r
QAM %QAM调制方式* d7 N7 D% v( ?/ c J, G$ E( u$ N
OFDM %OFDM调制方式6 L, t) |- `% Y. S% g4 i
Analysis %QAM和OFDM两种方式对比
( u6 m2 r" b4 L/ D
1 {% n$ d: g# [ Z. A1setup %初始化部分
0 B& H- @6 _" x% setup
+ Z* ^4 a* H! @disp(' '), disp('------------------------------------------------------------')
^ Q5 I/ E3 @; c/ xdisp('Simulation Setup')
( b* E; ^0 @, I) c1 d
, a6 T$ F4 W F# J% OFDM Setup -----------------------------------------------------------( l$ l$ b% T- u+ O6 P
fft_size = 128 % should be a power of 2 for fast computation: D! E# _' F" y/ Y6 x
% more points = more time domain samples (smoother & more cycles)9 A! g* b5 ^. F
num_carriers = 32 % should be <= fft_size/4
4 {9 ]: d0 c. j$ R9 y2 }8 M % number of carriers used for each data chunk
4 h2 y% M" m0 }0 E/ E" Z. E% new var - denotes even spacing or variations of carriers among fft points+ Z" l7 D/ C) |9 \0 Y1 H
input_type = 2;4 r$ C' M7 L3 y9 a: J. g/ G7 K
% 1 = test input5 ~: W7 c& R! m( p; F4 W/ f4 U
test_input_type = 1;
8 ?( s$ x/ \; k q3 G % 1 = bit specified (binary)" L) u2 @/ p, a) Q' j( n" O
binary_data = [0 1 0 1 0 1 0 1];
( ^' @0 j8 ?3 J6 S4 d0 H % 2 = random data stream (samples in the range of 0-255)8 v* Q. o, \. H' ?. j! w* K8 y
num_symbols = 9;
! M7 Y7 f4 @5 q" r F6 m % 3 = sinusoidal
. e- f- z9 } c( P$ H$ Z" N frequency = 2;
5 v. c: W9 n3 K8 e/ z num_samples = 50;9 u- v% g+ Y7 c
% 2 = external file input
6 z/ M& i1 B9 {( q file_name = 'shortest.wav'; % Name of input file/ w! J7 W4 p/ K& o
file_input_type = 3;! S% h0 d% w$ k3 n
% 1 = binary (not implemented)
/ j: J, Z3 d; J. w8 j# Z, l2 Q % 2 = text % Demo file: 'text.txt': F3 A* f6 n5 \1 d1 s
% 3 = sound % Demo files: 'shortest.wav' & 'shorter.wav'# z" F6 d7 Y- k# D C
% 4 = image (not implemented)
/ \1 i* G0 j5 c* H% l
/ g7 g; j- O c+ s% QAM Setup ------------------------------------------------------------" I7 u8 d6 f$ p4 E& Q# Y j
do_QAM = 1; % (1=on, 0=off)
' b3 v6 K& u: l# v+ gQAM_periods = 10; % defines the number of periods per QAM Symbos (1=2*pi)
$ y+ [, W% `. O3 x/ |# s }, \ e I; v6 Z- }, [4 v
% Channel Simulation Parameters --------------------------------------------+ n% g4 g( E* v4 S6 M3 B1 l
channel_on = 1; % 1=on, 0=off" b9 u' h. a4 n$ z# j: U' L; `
clip_level = 1.0; % 0.0 - 1.0 (0-100%)2 W, p& w% X; ^7 f" s% h$ e
% Max magnitude of the signal is 'clip_level' times the full magnitude of the signal" n; b% s+ Q5 z. A* d
noise_level = 0.0; % 0.0 - 1.0 (0-100%)
' c T: w$ S% W) A8 W. P/ u. j" H- p % Magnitude of noise is 'noise_level' times the magnitude of the signal! b+ u1 b, V! o
% Multipath Channel Simulation! L3 }" ^. Y- u. n' w/ s' ?
% Good defaults when fft_size = 128 and num_carriers = 32:' k0 x6 G4 U& d5 X
% d1=6; a1=0.30; d2=10; a2=0.25
- s4 L1 Q4 _" z) W9 _+ i0 j, X d1 = 6; % delay in units# {$ [" `1 g4 B j" s- g; W& T! E% t
a1 = 0.30; % attenuation factor - multipath signal is x% of size or original signal
$ I! l& f2 x5 |' X: r d2 = 10; % delay for second multipath signal) I- [0 w* J4 b8 c3 b; h( p$ S2 X
a2 = 0.25; % attenuation factor for second multipath signal
, c. Z; D0 P; S% F: a; B
! Z% g; _# D& ?+ D& W- J, q
$ }/ x; p3 {, J) V2 [% ****************** TEST INPUT SETUP - DO NOT MODIFY **************************
. }* E! ?5 Y0 y; D3 hif input_type == 14 c7 P! f# D* ]& a% L
if test_input_type == 14 Z# z- `3 L+ p, X# g) @
%specify BINARY input bit-by-bit& {8 G }) V/ `2 o7 H2 Y
data_in = binary_data;
" b/ L! y% m) Y/ }6 E end! `* w; t* C( `. O
if test_input_type == 2
$ y* _9 k' N, Q0 Y/ H %random input defined by parameters& Y- r4 ?( d6 x$ l
num_levels = 255; %number of possible levels of a symbol
1 D! N7 P/ B. b9 j2 w Z2 v- {+ J- \ %must be integer between 1-2556 U3 g8 B: `6 Z
data_samples = round(rand(1,num_symbols)*(num_levels-1));1 c( O# Y8 O- P" q
data_in = zeros(1,8*length(data_samples));+ I! e/ ?8 V: A: C7 B
for i = 1:length(data_samples)
# f# N: d( o: u0 t/ ^ data_in(1 + (i-1)*8: (i-1)*8 + 8) = eight2bin(data_samples(i));: Z; W6 F& t! b, x% |
end
7 t$ `! z* f* d) V6 W3 D end
. h/ t) H; K s if test_input_type == 3' q8 `3 Y- i% J
%data stream represents sine wave samples+ B2 z; v, `# G0 S/ D0 B# R* y: C
t = linspace(0,1,num_symbols); %evenly space number of samples$ D2 l+ g; A7 I; E. i2 S& n
%take 8-bit samples of sine wave# z5 [& R- h) M" v+ Z
data_samples = round(127.5*sin(frequency*2*pi*t) +127.5);
# {# V$ p) Q6 R ^ data_in = zeros(1,8*length(data_samples));
5 k2 v2 J9 f1 \- ]+ } S4 O for i = 1:length(data_samples) q) e) ^' p' G6 E
data_in(1 + (i-1)*8: (i-1)*8 + 8) = eight2bin(data_samples(i));3 E+ i( P4 a# E& p* G+ J8 e
end8 t7 _7 c6 H1 _' g0 C2 j3 a
end$ k1 s4 A/ I. r1 ?
end
6 \5 {5 s/ W/ X& B/ h* a+ ?% r- q' b) W+ s" w) \
already_made_noise = 0; % initialization (don't change)% Q, o% g2 b* c; O( q) V& R
1 f5 o0 B8 [" A2 x; @2、QAM.m
, p5 n, K3 W9 y# y% F `% QAM.m compares OFDM (multicarrier) to multi-level QAM (single carrier)
/ E4 t" @9 N: Z% when they transmit the same # of bits in a given time period
1 I6 }" z) N: x' t* n3 m) G" `0 R8 J. u, f8 [- Q
read % read data for QAM - does not affect OFDM
7 @3 t. X/ f% Y0 k- [' r5 ?, Wdata_in_pol = bin2pol(data_in); % Converts binary data to polar data
) o, R: Y! s7 @& X5 N, g! f& a! u
% check to see if num_carriers is a power of 2
8 \! n9 R `1 i/ e& I+ iis_pow_2 = num_carriers;7 n$ A6 U- x; X4 j7 N- e N
temp_do_QAM = 0;/ ?$ O5 ~/ a' O% [6 c$ A) D: y5 C
if is_pow_2 ~= 2
% j7 w2 a; l; N, S8 M. g0 E" A while temp_do_QAM == 0% }& O: U' k$ z1 |
temp_do_QAM = rem(is_pow_2,2);
6 ~$ b/ h" N! K is_pow_2 = is_pow_2/2;
/ U7 u3 t. [7 w: J+ O1 p if is_pow_2 == 2
8 F0 E; i1 X+ W temp_do_QAM = -99; % it is a power of 2 -> can do QAM
* h; v; b4 H. p G$ Y7 I# u2 s4 Q- b( A. H end
" n5 l$ _( K/ P* |0 q* m end8 H" C6 a3 \( W5 s& a c c
else
# l' y/ E; D/ I1 ]" l- X temp_do_QAM = -99; % 2 is a power of 21 H# t9 Z5 ^; o7 X5 C2 h) f
end" E6 I+ m& E- W! k& k; y
if temp_do_QAM ~= -99
7 q; k( `! {# Z5 j* S do_QAM = 0; % don't do it if it's not possible6 R5 ?5 ^ z4 H) m
disp(' '),disp('ERROR: Cannot run QAM because num_carriers is not valid.')1 f) G/ _6 Q7 \
disp(' Please see "setup.m" for details.'), a. A* T M3 Q8 j% e4 x$ I$ n
end
( ^5 B) B+ F" H; C2 f8 B" g) s6 n8 J5 `, c1 ~
3 ^) H. y1 H. @' A) c3 Kif do_QAM == 14 v4 }- P+ ~1 w3 R. j3 H
tic % Start stopwatch to calculate how long QAM simulation takes" T2 Z3 a+ g0 {" Y
4 Z/ W; i, D. z+ Z c- h) p disp(' '), disp('------------------------------------------------------------')5 X6 f6 }9 T. k/ @+ M
disp('QAM simulation'), disp('Transmitting')7 z- J( ^: W/ x
+ D$ |# n0 p- d/ ?
% Pad with zeros so data can be divided evenly+ z* w- e/ f6 y8 f8 K% r' P
data_length = length(data_in_pol);
% L) g8 B8 @# a1 z" v, F r = rem(data_length,num_carriers);
8 c [8 I& C* F( N if r ~= 0
( o8 o" G; L1 O# @/ W, R9 v( U for i = 1:num_carriers-r
6 v& s& }, c* Z' K9 d. ?8 R6 d data_in_pol(data_length+i) = 0; %pad input with zeros to complete last data set
/ {2 D0 c; r6 s7 v5 F& s end %speed improve possible0 ^( Y1 v9 l& Y/ W! n
end: f7 X0 k7 f% B3 }% @
data_length = length(data_in_pol); %update after padding; ]+ w0 c. G7 j3 a. u
T6 x# Y1 `, u# | m
num_OFDM_symbols = ceil(data_length / (2*num_carriers)); {: Q. ]' U, [% W& Q
% num QAM symbols that represent equal amount of data to one OFDM symbol
- v& x5 P0 Q; C num_QAM_symbols = num_carriers / 2;: [2 }* r$ a4 D/ F
% num samples per QAM symbol
7 W$ O6 r# j1 k1 i. p- I num_symbol_samples = fft_size / num_QAM_symbols;
: B7 G+ \ N* j5 r, i8 \ 2 C; k6 w; s& K/ [6 ^* O- u. J& }
% convert polar data [-1, 1] to 4 level data [-3, -1, 1, 3]' d1 V) J: ^% L* N9 O) d2 c
data_in_4 = zeros(1,data_length/2);
0 a. y; y: |& U# W" h; F7 C9 Z for i = 1:2:data_length
* q9 w5 E8 f; ]( G& q4 Q0 l( [ data_in_4(i - (i-1)/2) = data_in_pol(i)*2 + data_in_pol(i+1);
: ^; P2 v4 h w, O end& e( u% x+ \, c/ @
5 c) S" G9 |( J& M8 N
% define sample points between 0 and 2*pi' W1 K+ U! {7 w. |
ts = linspace(0, 2*pi*QAM_periods, num_symbol_samples+1);; ^0 l5 q$ |! W) l
0 [9 {$ C# B7 C" z) |
% Generate 16-QAM data) @$ v4 w; T4 }
% total length of 16-QAM transmission( D, L" m+ ?8 k; A
tx_length = num_OFDM_symbols * num_QAM_symbols * num_symbol_samples;$ R! _ K2 L2 {/ q7 P$ f
QAM_tx_data = zeros(1,tx_length);
3 T* M1 R3 Z% q3 X* \$ l5 N; c3 y for i = 1:2:data_length/28 b P' q: i! t6 i' \( T6 P7 s
for k = 1:num_symbol_samples& h/ E+ C4 p6 H" u* [7 f" Z! f7 D
QAM_tx_data(k+((i-1)/2)*num_symbol_samples) = data_in_4(i)*cos(ts(k)) + data_in_4(i+1)*sin(ts(k));$ a# V% [+ p9 N
end
9 a6 [; P4 m) Q* Z+ d# @+ O9 J end" p6 f8 p3 Z; c! A; _2 \
- g3 y6 z- N- E. t
% Do channel simulation on QAM data
' _ F6 C) F/ y xmit = QAM_tx_data; % ch uses 'xmit' data and returns 'recv'% Z4 D5 R8 [' r6 g
ch
; L0 I# E) u- m5 x QAM_rx_data = recv; % save QAM data after channel2 f0 [% w# D: I# i# d( ^
clear recv % remove 'recv' so it won't inteRFere with OFDM
6 y( L. r2 n$ N- ^3 `- C clear xmit % remove 'xmit' so it won't interfere with OFDM9 R& Y6 G# v% O. H! W l7 f0 i
( k+ _( Z3 ^! i- j, F
disp('Receiving') % Recover Binary data (Decode QAM)+ L6 ~& i" K) ]5 M% D% ~
cos_temp = zeros(1,num_symbol_samples); %
. o+ s" n c/ f8 `0 f F2 E sin_temp = cos_temp; %
' U: ` W# r' n. N8 Z" H1 v; a6 q8 ?! h xxx = zeros(1,data_length/4); % Initialize to zeros for speed
3 q. e7 N! x% f1 S: i yyy = xxx; %+ p; b* \1 C L3 |5 _2 p! k
QAM_data_out_4 = zeros(1,data_length/2); %
" K$ Z5 B/ d9 N ; m/ e6 R9 n V# ^: G
for i = 1:2:data_length/2 % "cheating"& ~+ S( F1 O7 J3 T- U
for k = 1:num_symbol_samples
# }6 ^3 k; K- U % multiply by carriers to produce high frequency term and original data; G3 m- W) M; r3 C- S! Y$ c
cos_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) * cos(ts(k));
) b9 |" ?% N y sin_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) * sin(ts(k));# n$ K( M/ ~, v
end
' F) _6 J1 Y/ |/ J" l" Q# A. D+ Y % LPF and decide - we will do very simple LPF by averaging4 I6 H0 Q! Q1 s
xxx(1+(i-1)/2) = mean(cos_temp);7 K) q& N1 m, ^9 n" c- l+ w& l
yyy(1+(i-1)/2) = mean(sin_temp);
/ G; T! @- `# Y) [ % Reconstruct data in serial form# E9 U, c1 ^4 h4 o2 ~: H
QAM_data_out_4(i) = xxx(1+(i-1)/2);3 X- ]& w0 t0 _3 d/ u3 f
QAM_data_out_4(i+1) = yyy(1+(i-1)/2);
: n0 a: P7 E: d; N end
- e5 ?0 U9 w3 Z+ l# O% a2 Y - L+ j/ C( [' X% g5 U. A
% Make decision between [-3, -1, 1, 3]6 {9 J% B- U L- d9 a8 g
for i = 1:data_length/2
6 E# W0 G) `' _+ _$ X if QAM_data_out_4(i) >= 1, QAM_data_out_4(i) = 3;
& W; |' D4 E- k/ a5 _) \9 s elseif QAM_data_out_4(i) >= 0, QAM_data_out_4(i) = 1;" D/ q, L6 t) j/ y
elseif QAM_data_out_4(i) >= -1, QAM_data_out_4(i) = -1;
% B. E2 a% c# R2 I# Y$ R% O8 l: L else QAM_data_out_4(i) = -3;0 h f/ w0 Z0 R5 Q1 M% ^: g3 V9 x
end
- s8 V/ E, f+ t+ \ end
3 M! w; ^5 U$ U- |0 s% I+ w % e3 w% w% x! q% l/ Y2 F
% Convert 4 level data [-3, -1, 1, 3] back to polar data [-1, 1]
7 D) V; {) N) v' a QAM_data_out_pol = zeros(1,data_length); % "cheating"
2 d6 n7 y2 ]) \, T' C+ O( N0 h% m for i = 1:2:data_length) c+ e$ A x( k, Q- a7 F' V
switch QAM_data_out_4(1 + (i-1)/2)
& t. V! |: A. J. ` case -38 @( D& `. O: V7 `
QAM_data_out_pol(i) = -1;
/ N+ `0 f0 N) i( B4 X" Z$ |) j- }, k' T QAM_data_out_pol(i+1) = -1;# {0 T+ @# z% ~. z. q
case -1
+ j+ Y' @8 [5 `" l QAM_data_out_pol(i) = -1;$ y* h1 k/ s3 q. l6 L* O0 r" c* X: h
QAM_data_out_pol(i+1) = 1;" P7 p2 q0 q# K! F! Q/ o
case 1
1 [% O/ i4 p; B7 _+ z# I3 D* ] QAM_data_out_pol(i) = 1;
3 K7 y1 w5 x% b( i& B ~ ?8 w QAM_data_out_pol(i+1) = -1;
) a! z) A0 x: c2 [0 R! m' i case 3) w" L8 b" G/ [! s4 O
QAM_data_out_pol(i) = 1;9 E' Y- \3 B6 F. @2 D
QAM_data_out_pol(i+1) = 1;# G- Z! [9 y1 |8 d
otherwise
; ]* `+ a" x+ Q! d disp('Error detected in switch statment - This should not be happening.');
6 x+ e7 j$ p2 g( f: ~9 B. n; d end
, X u6 B" t( _2 p% J end0 t; F" U% C- Z% t' e
QAM_data_out = pol2bin(QAM_data_out_pol); % convert back to binary- w: s/ d: M; ~
4 m& V- z, b1 { % Stop stopwatch to calculate how long QAM simulation takes8 v, @- B m0 G* M
QAM_simulation_time = toc;
. s4 g: S8 X' n2 E4 \ if QAM_simulation_time > 60' C3 ~, P c, V2 v
disp(strcat('Time for QAM simulation=', num2str(QAM_simulation_time/60), ' minutes.'));, Y9 Q" P" l0 N3 Y+ a1 g
else# ]; z+ \# M8 r$ L
disp(strcat('Time for QAM simulation=', num2str(QAM_simulation_time), ' seconds.'));
! a, Z; Z6 ~4 ?# ^9 Y0 m end& y: H+ h& n1 ?3 c3 s1 |6 i
end, O- r+ E5 }/ K9 K
- X4 X1 f: s |7 n9 \/ D2 j' D3 d3 % Run OFDM simulation
) @4 u) ]' \8 `4 ntic % Start stopwatch to calculate how long QAM simulation takes" N) }, g$ D- I) t
disp(' '),disp('------------------------------------------------------------')
0 b! |3 R4 U" E( J; z* t" }1 ]disp('OFDM Simulation')
9 q _( z0 m( l9 s& K3 vtx
- Y5 B+ j4 L/ R: @1 a" ech% U0 |+ q+ e" d7 ]1 X% d) A
rx2 v2 F. K# V0 b1 ^ w; C* v
% Stop stopwatch to calculate how long QAM simulation takes# A% v5 |6 o p) C7 J
OFDM_simulation_time = toc;
+ m$ _0 U* K4 y8 I" `, oif OFDM_simulation_time > 60; [/ s) B. J. Z( a
disp(strcat('Time for OFDM simulation=', num2str(OFDM_simulation_time/60), ' minutes.'));0 |( O( A" \, G6 E$ L+ w
else9 E1 h- I& J" I
disp(strcat('Time for OFDM simulation=', num2str(OFDM_simulation_time), ' seconds.'));
) \: h0 @" r" K' }( Gend! ~+ f" Z/ W5 w) |* p$ d" s! u
% T [# K% D* j3.1发送+ V0 v# O) i. K6 V2 \
% tx
, M* A( w" u( @+ i+ L" Fdisp('Transmitting')' ^" ^+ k# R/ G$ D, w
read %read original data X f! \. F$ i$ Y# A
data_in_pol = bin2pol(data_in); % Converts binary data to polar data
9 W- ]/ ]$ l$ D5 `7 P" v' etx_chunk %convert polar data into chunks. * o4 j" r0 ^+ |2 x0 F
: G$ q: }$ ~% t% v* p% perform ifft to create time domain waveform representing data: k) z% ?& m6 J" n# r- {
td_sets = zeros(num_chunks,fft_size);
2 L2 H; |6 F9 a8 V( f! tfor i = 1:num_chunks+ Q7 @% q* g4 N) U8 o8 {3 L
td_sets(i,1:fft_size) = real(ifft(spaced_chunks(i,1:fft_size)));! |+ p9 i3 e+ a- @+ w+ u
end5 i. n: m& K6 I% v9 v" `) G/ X
0 O) @% m1 L4 W& _
tx_dechunk % Construct signal to transmit by placing time domain sets in series9 c& i/ b8 D. d9 q
9 D, T/ ^; g% v/ l; t! I9 N3.1.1 % tx_chunk %双极性数组转化成OFDM字符串
6 N9 F/ j6 v% R4 e- Tdata_length = length(data_in_pol) %number of symbols in original input
0 z, k) }6 d8 t* a; V( Hnum_carriers
4 A' m6 a7 A3 d6 J6 pnum_chunks = ceil(data_length/(2*num_carriers)) %2 data on each carrier (real and imaginary)
; ~4 ]2 j% [ V* \( ^6 ]r = rem(data_length,2*num_carriers)3 e" c7 }, \' r& w9 P( D, T3 l
if r ~= 0& X: [ t$ Q; V7 f8 V
for i = 1:num_carriers*2-r
8 i2 x1 j, T$ N' w' u+ ^: ? data_in_pol(data_length+i) = 0; %pad input with zeros to complete last data set8 L6 k6 j- e; L! [ K
end %speed improve possible1 {# h; @" V# x; e/ {
end% q u) l+ u$ d8 C8 s- @' D# l
& l# z+ G7 m4 v* E' R4 K% @% [% break data into chunks
Q2 I) D7 U' Ochunks = zeros(num_chunks,num_carriers); % for speed
1 u. e2 r& b& @! C! [$ j5 J1 Ofor i = 1:num_chunks
2 h- N+ |( O, K9 X- C % *********************chunk done1 g1 f: U" z! |: g" O
for k = 1:num_carriers
- D( j! w5 w4 p: q! D chunks(i,k) = data_in_pol(2*num_carriers*(i-1)+k) + data_in_pol(2*num_carriers*(i-1)+k+num_carriers)*j;
8 Y$ C3 u, y9 w j% A, A end
5 Y( k5 u+ M2 R9 o2 W; P: ^. oend" }0 U% \& }6 M: \: K8 N3 O
, l' X" G5 D1 T
chunks# N8 P8 a+ a3 k3 H: x
% Padding chunks with zeros so num_carriers and fft_size are compatible
% v9 S& j q6 \# ?9 j% Once compatible, further spacing is simplified" m$ X) h, X! I2 @2 l; S) }/ S
num_desired_carriers = num_carriers;" J* n3 Y8 \3 x! Z) u+ q: K9 ^
num_zeros = 0;' ~1 I0 `8 |+ [9 h" u( f1 @5 t
thinking = 1;
0 o! Y+ L0 r% \1 _, b+ h2 Q& Wwhile thinking == 1 % Continue if num_carriers and fft_size are not compatible; S: N/ m% L6 m
if rem(fft_size/2,num_desired_carriers) == 0
& r* W3 D' p2 R+ @. v* ~0 \ q$ K thinking = 0;
1 L( C' |# j: G4 d0 m7 k/ [# U3 R) P; v else6 A; _' u5 ]2 T2 \& M( @5 W* P
num_desired_carriers = num_desired_carriers + 1;
3 ^2 n. w5 }5 p" m0 t9 ^' Z num_zeros = num_zeros + 1;
5 @# g4 s: G; c! {1 _9 w O" I& Z4 o end
% j. D5 ~1 l( |5 Send# v; y% Q" X6 U1 O
- a3 M( D* V5 P) x/ Qpadded_chunks = zeros(num_chunks,num_carriers + num_zeros); % for speed7 R7 Z0 w$ f; U/ w- L h8 Z
padded_chunks(1:num_chunks,num_zeros + 1:num_carriers + num_zeros) = chunks;
& w! ~8 H2 ?6 p4 u, q7 c8 U
2 I" W- ~/ h8 ?/ {3 ~ k
* h0 ^4 u$ y8 `, Z/ Z' N5 O%compute zeros_between1 ~* ?, O4 k6 \* p
zeros_between = ((fft_size/2) - (num_carriers + num_zeros))/(num_carriers + num_zeros);; t/ A1 R) J+ i2 J/ p
" c( w: n7 q0 M. E) `+ a0 R/ mspaced_chunks = zeros(num_chunks,fft_size); % for speed - extra room for folding later
/ f7 S w @# }7 F5 O%add zeros_between
* G8 X! J! ~6 t! j! p0 G+ G3 ?) Li = 1;7 u {- k0 e! |: d' H
for k = zeros_between +1:zeros_between +1:fft_size/2% t1 N& ~( X0 V* h
spaced_chunks(1:num_chunks,k) = padded_chunks(1:num_chunks,i);
- P# v+ U4 G- F/ J. Y0 | i = i+1;" p# J8 @- k6 q- w( R
end
1 b6 B6 m5 T* d" o9 c+ n; S: w9 x! M- ~3 w; \4 }
% folding data to produce an odd function for ifft input
# h- j/ f0 P: t4 q$ e. _; Ffor i = 1:num_chunks
( Q$ Q* v) R! w % Note: index = 1 is actually DC freq for ifft -> it does not get copied over y-axis+ h4 d' @& `5 i O" V; a
spaced_chunks(i,fft_size:-1:fft_size/2+2) = conj(spaced_chunks(i,2:fft_size/2));# ]+ n) K# u+ H2 r2 E; ]
end
, d* A! u1 e. `4 u- J* |& h
" A: d2 ?) N6 p0 f. ?" \4 L3.1.2 tx_dechunk8 a3 Z: r6 T/ r& |+ t( d& f+ [* i1 M
% tx_dechunk
; R7 ]8 L# b2 P5 w( T
4 p, i, L2 E6 C4 X$ x5 y% T% Construct signal to transmit by placing time domain sets in series1 F7 P+ ^3 k3 d/ D7 c
xmit = zeros(1,num_chunks*fft_size);; { `4 X/ }1 t- X0 r
for i = 1:num_chunks$ ]5 U# D- `5 y& Q
for k = 1:fft_size3 D8 Q9 L; ?: s# I
xmit(k + (i-1)*fft_size) = td_sets(i,k);9 l j, K( k/ {( [3 b0 G" S* w
end1 x7 }0 B/ } {8 o
end
9 G' w: m" u! R* Z% _( A# ]9 V) F. N) L- U# T
3.2 %ch.m
4 j4 \# z5 }9 U( ]6 U o6 H- F% ch9 C" [3 k' S2 f4 m- y$ v- C
recv = xmit; % channel is applied to recv, don't modify transmitted data' j9 U$ G0 l/ P( n; j
if channel_on == 1& G |+ P j* Q" u
disp('Simulating Channel')
, n. ] S, n9 l' F& i$ l norm_factor = max(abs(recv)); % Normalize all data before applying
1 o& V0 {+ e$ A* C recv = (1/norm_factor) * recv; % channel for a fair comparison
/ }! L( ?( B Q ch_clipping %clipp data
; |- J; O2 o3 M: I2 c# M ch_multipath %# }1 p/ c# a1 C
ch_noise %5 w1 s* ~' ?0 r( q1 K7 [0 M
recv = norm_factor * recv; % Restore data magnitude for proper decoding2 e. ?& q( P2 b: o5 t7 r1 U8 }
end
9 r1 R. H. [+ ~& A0 L; j! V% Y
3.2.1 %ch_clipping.m$ z, l. _. b8 y- `9 N, }
% ch_clipping
! X7 C3 a* c5 b3 t9 d' o8 rfor i = 1:length(recv)* u8 f" i. j7 f
if recv(i) > clip_level
7 D( C. b/ N e/ C1 Y0 j recv(i) = clip_level;
7 Y; y. z4 L0 B. \+ W end
& \* j( ?9 X6 ~; D: |( X if recv(i) < -clip_level
( F0 S% K8 Y" v5 ^ recv(i) = -clip_level;4 z7 `4 V N1 i5 L2 g
end
5 q: Y/ y& d! h$ X9 t4 k5 C* fend
+ _& Z) S% v- X& x- _
: o" Y: a( F0 G" ^6 T. S7 U! }3.2.2 % ch_multipath % 产生多经的方法 S! }+ a1 D7 p: h9 F
copy1=zeros(size(recv));0 c' L6 ~- x5 @9 P. x# x
for i=1+d1: length(recv)
5 I( i& [0 O! |6 h5 h* A copy1(i)=a1*recv(i-d1);3 x8 @ X7 k1 \2 P% c0 Q8 s
end0 ?* Q: }& K+ Y g
copy2=zeros(size(recv));
) R) M7 z5 Y. L efor i=1+d2: length(recv)6 }% h$ H7 A3 @- a2 j& `4 `
copy2(i)=a2*recv(i-d2);5 D0 o9 |! z% T# _3 k* T' _/ Q% V
end0 `9 Y* s/ S0 q
recv=recv+copy1+copy2;
# q1 i, A1 p1 X, S% ?; q( |7 `$ {9 n& @: C! f
3.2.3、%ch_noise %施加信道噪声; A* K8 l6 r! _ D5 i# R
% ch_noise (operate on recv)3 q0 Z1 F# z. O$ n. Y. q+ ^
% random noise defined by noise_level amplitude5 `+ v: |6 z% J
if already_made_noise == 0 % only generate once and use for both QAM and OFDM
3 v, U! l) N3 J" D% f noise = (rand(1,length(recv))-0.5)*2*noise_level;
2 S7 x2 D6 [% s4 S already_made_noise = 1;; B( ~! }. V# {7 P; F
end
0 q6 _/ D! n1 O _recv = recv + noise;4 V( n4 n# k; g0 t
! y: I/ i, k; ]" w2 x" `
3.3 %rx.m %接收程序9 Z( g0 q; D* {: I A$ n0 J% q9 v
% rx6 j3 l4 W- x4 R: Q& Y
disp('Receiving')
1 R* ~6 v3 J3 S( D5 D4 {/ z7 nrx_chunk5 j8 m3 l4 A+ o2 R9 L- U
6 I: w& B& ^ F# }) i* H
% perform fft to recover original data from time domain sets
) [3 v: f/ z3 brecv_spaced_chunks = zeros(num_chunks,fft_size);2 I' S7 {0 B/ z
for i = 1:num_chunks
4 n& L% ?% j+ ~# {+ n: E recv_spaced_chunks(i,1: fft_size) = fft(recv_td_sets(i,1: fft_size));
$ V w2 P+ W2 ]) N& j! F3 g7 P % Note: 'round()' gets rid of small numerical error in Matlab but a threshold will be needed for a practical system
) {8 y8 \) I6 f" V- w/ a % 2001-4-17 -- Got rid of 'round()' to do decoding more intelligently9 i% z/ ~6 u/ d/ S2 x. ^* H* t
end
7 z# Z- q+ U6 S% Grx_dechunk
5 a' M* w0 J. T* e8 \- v- poutput = pol2bin(output); % Converts polar to binary
; A- q, G7 s, g1 q! Pwrite9 R' C t3 S. j, y9 ~3 |$ p. Q* W
j7 J7 n9 _" y3.3.1 %rx_chunk.m: G2 U9 d0 a$ `& V
% rx_chunk
" N+ [1 i' _/ G% M7 w9 f% break received signal into parellel sets for demodulation, D; @8 E3 |5 W0 ?+ Z
recv_td_sets = zeros(num_chunks,fft_size);
( T& U6 x" O( [& j, z+ W# K7 @for i = 1:num_chunks
5 Y; T+ b- h% g/ b8 x+ R3 Q for k = 1: fft_size
) A0 g( K @$ A5 X2 w# \ recv_td_sets(i,k) = recv(k + (i-1)*fft_size);* l' |1 l, s( Q5 G! Z
end
/ X- r8 P, v2 W6 p+ S. I: uend7 \/ K4 L/ o2 i
4 [% ]( _' D2 R1 D
3.3.2 % rx_dechunk %并串转换
+ f7 j/ S7 q# I% rx_dechunk
" h7 b$ g' O$ T6 q7 U# E( U( s% take out zeros_between from recv_spaced_chunks --> recv_padded_chunks7 u _- ?/ G |9 j8 {) L
recv_padded_chunks = zeros(num_chunks, num_carriers+num_zeros);, l& F5 Z1 i# e2 |
i = 1;
* \! D" _$ C) u7 A! h: C2 \2 @% Xfor k = zeros_between +1:zeros_between +1:fft_size/2
8 ]0 ^3 V! x$ T6 p8 f# z3 h recv_padded_chunks(1:num_chunks,i) = recv_spaced_chunks(1:num_chunks,k);
# O( c1 ~; I7 n o i = i+1;6 n% |: o8 d$ J, b0 d$ b/ B" [
end
z! C, ]6 D7 J3 N* V
9 v1 C7 m# t/ V5 V% x' V2 u% take out num_zeros from padded chunks --> recv_chunks
% Z2 Q! q% C& X P+ B; Grecv_chunks = zeros(num_chunks, num_carriers);/ E$ u, {9 w( y- O
recv_chunks = recv_padded_chunks(1:num_chunks, num_zeros+1:num_carriers+num_zeros);
, @' P7 ?7 Q3 f& Z2 C' |* D+ l* L
0 Z q$ n% }2 o9 ~7 T1 a% Recover bit stream by placing reconstructed frequency domain data in series
" d7 c4 V* }; z6 |; Z/ k' srecv_dechunked = zeros(1, num_chunks*num_carriers);
% {9 m* X3 H* m9 _% H' H' b' X+ ifor i = 1:num_chunks9 R. N% g% R- X2 a) L. Q
for k = 1:num_carriers* S* W# q4 e2 e/ h. Y
recv_dechunked(k + (i-1)*num_carriers*2) = real(recv_chunks(i,k));
6 \7 H! v' n6 ?' @; ~* K! P1 a recv_dechunked(k + (i-1)*num_carriers*2 + num_carriers) = imag(recv_chunks(i,k));5 `5 ^- O) t$ \0 o$ K) R z
end; L0 e4 w @. S" ]+ w6 z, g, {) y
end
1 u6 F9 E6 y T2 M$ [+ M$ N2 i2 T* u p. T2 z3 _8 \- V
% take out trailing zeros from output --> output
: B$ y3 @+ M. Coutput_analog = recv_dechunked(1:data_length);$ |: \+ |2 L9 \. M
output = sign(output_analog);
1 B+ D1 N$ o$ J% |; V: s
; C7 Z. k# O4 v" a3.3.3 %write %save received data
: c/ D' L- k J3 T9 R$ `% write3 e: h* p4 }: ~1 r$ |0 ]
% ******************TEST OUTPUT*********************************3 }( g% }3 r7 |1 W& _
if input_type == 1- b1 _% z6 N u; ~9 ~6 G0 F
if test_input_type == 1' k) m- e3 @' |# E
%already binary - do nothing
, _ j& a, A& U' s5 j( Z end9 q6 b1 {# J7 H
0 }3 x9 ~$ l2 V5 t& r
if (test_input_type == 2) | (test_input_type == 3)
% ^: E" f! W8 ^2 F% \' L %random input OR sine wave samples
9 {! k Z7 F$ b+ i5 c% n; \# ]. s output_samples = zeros(1,floor(length(output)/8)); %extra zeros are not original data* ^6 Q& u, H3 ^$ V8 ?8 ^) }/ K2 ^
for i = 1:length(output_samples)
# v2 ~5 f5 E1 w output_samples(i) = bin2eight(output(1 + (i-1)*8: (i-1)*8 + 8));% H9 x$ H. N, A0 K: _' N; d% d
end5 P; r% X2 U) k* W3 u/ h; _
if do_QAM == 13 Q* D0 ~) J- }" u E, ?2 ~' i
QAM_output_samples = zeros(1,floor(length(QAM_data_out)/8));
5 w. t( d4 O* v1 g1 ^ for i = 1:length(QAM_output_samples)4 V2 V$ _8 G4 y
QAM_output_samples(i) = bin2eight(QAM_data_out(1 + (i-1)*8: (i-1)*8 + 8));
/ I, _+ B: ~! q9 l a$ R$ n% X) Y1 m) e end
5 H1 o$ L. N( e% z% d5 e$ x+ G& r end3 ^9 T0 V# U/ Y+ r0 x p( e: Q0 Q
end
1 z2 s& o' N: x" k' B( Xend2 Z$ v+ Y0 e: p4 d9 B0 z+ X
9 O2 N/ I+ t4 i; H' X* H% ******************FILE OUTPUT********************************* b; k$ ^2 o& C! C+ y7 E$ N# W* R
if input_type == 2" k* f# T8 ]6 h4 U3 L1 c, b
4 R! E& m/ `% w p if file_input_type == 1
/ ?2 L. p v5 ?' q; E5 }; [9 P %binary file output - not implemented
" V7 `, r- _' z' S end
" X1 t- M' }+ {( r0 W: q+ Y$ h" v* M
6 ^( P& I0 l8 }/ O$ A. W if file_input_type == 2
9 a, f, \1 a; Z/ M E$ }4 v %text file output
3 h% Y& T8 I% q& L) ]; e5 K output_samples = zeros(1,floor(length(output)/8)); %extra zeros are not original data% A" }2 i4 ]1 I- y X
for i = 1:length(output_samples)
4 J, L3 E6 i0 v+ F' w0 @' B output_samples(i) = bin2eight(output(1 + (i-1)*8: (i-1)*8 + 8));- w8 d2 i# A s% ^; i
end
6 H1 b5 X. {) V* v file = fopen('OFDM_text_out.txt','wt+');
3 U( [& [5 Q6 Z6 U$ d6 e fwrite(file,output_samples,'char');
5 T" F2 e4 o" i5 @& \ fclose(file);- o a5 ~1 ^; C$ \; ~2 y8 r
U+ x1 \7 Z8 u5 }5 a if do_QAM == 1" H# u5 U' M* u! _' e
%extra zeros are not original data
/ c9 f* {' G* K1 t; ^ QAM_output_samples = zeros(1,floor(length(QAM_data_out)/8));
" H. }% Y7 G; K1 y$ j $ T2 p8 |; H; `# A0 o
for i = 1:length(QAM_output_samples)
, Q( S+ G' v. s, {) s; }# _ QAM_output_samples(i) = bin2eight(QAM_data_out(1 + (i-1)*8: (i-1)*8 + 8));
/ e2 X; w: W( z end1 _1 u2 P/ `+ j( K, j7 ?* t4 @& `
file = fopen('QAM_text_out.txt','wt+');
. l' R8 C0 e+ J/ R5 G$ Z* R fwrite(file,QAM_output_samples,'char');) Q/ L+ N! o. B$ j+ Q7 x; u- Z
fclose(file);; I/ ^9 z( q) L5 {" F* N6 p
end% F2 j5 t& S% z7 Z# k; s2 R0 x
end" t0 J4 x. n( a4 m4 Y' c. e+ q
" ]/ O1 J4 T; L7 ]+ y if file_input_type == 3: L) ^% l: e7 ?, w/ p
output_samples_big = zeros(1,floor(length(output)/8)); %extra zeros are not original data9 ^. z$ d# `7 T& f
for i = 1:length(output_samples_big)
. _! M/ ?" ~) h( g o' Z: Q4 ] output_samples_big(i) = bin2eight(output(1 + (i-1)*8: (i-1)*8 + 8));* [! l5 l4 S& a& |. a$ D) f$ j
end( {, `/ t$ y; F/ e4 K6 E# l V& L
%convert dynamic range from 0:255 to -1:1
8 ^6 \: s( S, l) Z6 Q output_samples = (output_samples_big-127)/128;
, M& I# j* W8 ]0 {5 U2 w %sound file output
& S" S2 K8 u+ }* E' l& n# H wavwrite(output_samples, 11025, 8, 'OFDM_out.wav')
5 d! h6 ]9 M# y8 Z$ ~ if do_QAM == 1. q9 a% O8 W9 v! u
QAM_data_out_big = zeros(1,floor(length(QAM_data_out)/8));& R9 V. y, p1 J" L+ `% U
for i = 1:length(QAM_data_out_big)
* y0 a. F$ v* f/ |/ D0 A$ { QAM_data_out_big(i) = bin2eight(QAM_data_out(1 + (i-1)*8: (i-1)*8 + 8));7 \8 ^% P/ \+ w6 \2 P
end8 v# x V" K0 @1 E) B; k
%convert dynamic range from 0:255 to -1: 1
0 K6 C( S% Q4 {- G' O: _' ~0 [ QAM_output_samples = (QAM_data_out_big-127)/128;% B! i5 i9 w; r+ {; x7 s
%sound file output
1 Q9 H! u M* W9 U wavwrite(QAM_output_samples, 11025, 8, 'QAM_out.wav')) Q7 M* d' u' d8 O. U/ m6 O
end
) a: h# _, |6 f5 M end1 i5 S& T }/ \6 @
% c6 m3 l, o! W; h8 ]* m- g* Q if file_input_type == 4; E9 v+ [9 E+ Q" {
%image file output - not implemented ; |1 l6 P! _% h8 |* B5 V4 H
end: n; u/ f( _$ O2 w
end- E$ [ n( Y" F
3 q. M" v. k; L% k$ Z& o$ |# Z
4、%Analysis.m9 H4 U8 T/ _" W8 I: G) x
% Analysis
% X0 Z- {# Q1 B# E' g. R3 Y; tdisp(' '), disp('------------------------------------------------------------')
4 u7 |. T& m1 cdisp('Preparing Analysis')0 x8 d t3 y: D* O' o) l
figure(1), clf- ?. I9 P/ p: ~. }
if (input_type == 1) & (test_input_type == 1)
" J3 R( V: b# ^9 r- K subplot(221), stem(data_in), title('OFDM Binary Input Data');+ b+ n% i7 Y" I& g
subplot(223), stem(output), title('OFDM Recovered Binary Data') A9 h) x I- K& Y7 v
else! \6 l& B' Y0 r8 V4 I! ~9 x4 N
subplot(221), plot(data_samples), title('OFDM Symbol Input Data');
3 p( T' L0 M! q+ c0 ~3 O9 t subplot(223), plot(output_samples), title('OFDM Recovered Symbols');. y! Z, C% v3 f; e- N" V" ~" i
end
6 G* Y/ X, x, p& m. z* H8 Bsubplot(222), plot(xmit), title('Transmitted OFDM');/ F/ @9 \8 b0 D
subplot(224), plot(recv), title('Received OFDM'); ]* X t* r M1 [. c2 T" f3 h: I" m# @
/ B7 o# l5 z% m$ h8 f* r6 @7 h# f( {7 M6 d/ H8 S
% dig_x_axis = (1:length(QAM_tx_data))/length(QAM_tx_data);
! \! q0 o$ L ^* |% figure(4), clf, subplot(212)6 C5 `4 N* E/ _5 B" D* N
% freq_data = abs(fft(QAM_rx_data));% B- N" I4 f! z
% L = length(freq_data)/2;
- T/ [+ ?) {' R) f
" [8 a, \& I# l! |1 n- G/ adig_x_axis = (1: length(xmit))/length(xmit);2 M& k& ~8 y" M) S, U% x9 d
figure(2), clf
4 v7 r1 D; z6 C! p
: y/ `5 j4 \. ` d) w0 Xif channel_on ==1
3 N" |' z0 [' G" J num = [1, zeros(1, d1-1), a1, zeros(1, d2-d1-1), a2];# @! y, M& O5 y* i" Y
den = [1];0 T* U; H- L& P1 O- S1 v) V" j. C
[H, W] = freqz(num, den, 512);& V: M" k+ M9 C
mag = 20*log10(abs(H));
' C# n g% q* b/ K phase = angle(H) * 180/pi;3 U+ R7 ~6 o6 e3 [1 H. Q. B6 o
# O6 F& i# e' e$ w' f; V2 V
subplot(313)$ q9 a+ [- W: `- h5 x# V
freq_data = abs(fft(recv));* D9 X" u1 d; Q# v
L = length(freq_data)/2;
6 U) Z6 i) s6 u# |. |/ X plot(dig_x_axis(1: L), freq_data(1: L))9 [$ |8 _$ |5 r; I' z: C/ t
xlabel('FFT of Received OFDM')! i( v: f* e" K; k
axis_temp = axis;
- g( H% J4 d1 s
1 @' T3 t# J7 j subplot(311)," O- ~2 K' }0 H4 }: P$ ~0 p
freq_data = abs(fft(xmit));
8 M) u/ N$ q. }7 t' H5 Q plot(dig_x_axis(1: L), freq_data(1: L)), axis(axis_temp)' i9 i3 x' X. W, L2 e0 \
title('FFT of Transmitted OFDM')
' Y. J' s* m R3 n* Z) j
6 @( k1 z1 l' [1 ` subplot(312)
% E( O% z0 Y( R! _" E, C plot(W/(2*pi),mag),5 h& X2 G, C, i4 K
ylabel('Channel Magnitude Response')+ @3 X& y: z8 c# ?
else
4 D8 s& \4 ^/ P; A1 Q( P subplot(212)
7 ?* z% J! R) }8 `" ^! Z freq_data = abs(fft(recv));2 ~! W' ~' _! n# c8 |7 M
L = length(freq_data)/2;
$ ~3 H# x0 j- v) V: e* P plot(dig_x_axis(1: L), freq_data(1: L))& H1 X2 Z: ~0 V: {
xlabel('FFT of Received OFDM')2 [1 [, E& D/ ~% M, y8 {+ { B) J
axis_temp = axis;
6 |$ C! M% B5 y3 j7 Z, b# b
. q4 p) \! ?1 S5 Q& E subplot(211),
; l+ q W I5 q* x freq_data = abs(fft(xmit));# x. D1 D$ M' }- }' m! n% Q0 Z) \
plot(dig_x_axis(1: L), freq_data(1: L)), axis(axis_temp)
: U3 V, g% L2 C title('FFT of Transmitted OFDM')
$ w/ {' c! v7 y$ @0 aend+ Y2 o, G$ Q5 G. ]4 n8 ^9 W( {
& l. g+ ` b1 R9 j% W. o% if file_input_type == 4' c @$ G% S4 ^! d' n! M- U
% figure(5)5 K3 [1 W8 Y! l% A0 T
% subplot(211)5 T& Z# Q8 F& e3 |
% image(data_in);
, I+ @9 x: C) P+ p i; ~ A4 J/ f% colormap(map);
8 }& i% N+ w1 o3 Y% subplot(212)
! K/ K9 H R* U, O# J4 `% image(output);" f; ?7 }$ z4 n# ?8 X, D, W
% colormap(map);5 l$ Q7 v, H; M& i3 f( U
% end1 | Q8 K; C- b1 |' v- n
$ F/ D: W" L' }7 f5 M! A2 \4 k
if do_QAM == 1 % analyze if QAM was done
O- j( \3 X. P6 h & R+ \8 c0 b. c G2 S
figure(3), clf% O x; ~( z, ?- G8 m9 N6 i
if (input_type == 1) & (test_input_type == 1)
6 @) L8 Q9 ~# @8 t subplot(221), stem(data_in), title('QAM Binary Input Data');
2 }% w9 m7 f, j. ^( m3 { subplot(223), stem(QAM_data_out), title('QAM Recovered Binary Data')) T* i/ w* s \
else# y. k( ~, X1 ]" ^2 M6 x M9 I: g
subplot(221), plot(data_samples), title('QAM Symbol Input Data');
3 S3 @0 E( k0 k/ G! I! g7 ^ subplot(223), plot(QAM_output_samples), title('QAM Recovered Symbols');
2 _/ H, W1 Z# H* z7 Z/ h end o7 ]5 _1 q$ g% Z, t" s+ p
subplot(222), plot(QAM_tx_data), title('Transmitted QAM');
. F4 ^" Y" ^1 m x" c subplot(224), plot(QAM_rx_data), title('Received QAM');& E# M( v% r1 V" X
* ]: q4 `5 h/ Q% E
dig_x_axis = (1: length(QAM_tx_data))/length(QAM_tx_data);& m2 _7 K7 U/ }% A! f
figure(4), clf8 O0 y4 ^, r' \0 Y9 ?& D. w/ }
; _$ {+ x# P1 y/ o i2 s if channel_on ==1
3 ^+ Q9 Y5 q9 Y2 r* j subplot(313)1 D: Y0 @) [0 w, J6 W
freq_data = abs(fft(QAM_rx_data));4 A Y: A) s4 N1 v2 z4 H* Z' |
L = length(freq_data)/2;( `5 z) V: v( L0 F! r) f
plot(dig_x_axis(1: L), freq_data(1: L))
9 U# u+ R X0 r4 ?& f& [ xlabel('FFT of Received QAM')
- a3 F- L w) L) \+ ~8 G5 c; _ axis_temp = axis;
7 _9 b1 y |; U/ c0 C7 l 4 x# {. l ?! S' D. o
subplot(311),
. e( ?# K- A$ h. l freq_data = abs(fft(QAM_tx_data));$ a o: w; H O* c
plot(dig_x_axis(1: L),freq_data(1: L)), axis(axis_temp)% b. ^# K5 o4 ^. Y
title('FFT of Transmitted QAM')
. s3 P$ c2 F! M6 e" F0 T1 \/ D: r% s; N
( z' F' a; W! m% r6 l5 d subplot(312)4 k2 t7 ]8 B2 e$ Z; R. z
plot(W/(2*pi),mag)
% j& a, d0 e ] ylabel('Channel Magnitude Response')
0 D/ O4 n$ S z! W- Z else
C0 w5 b ]* Q1 z+ w2 j subplot(212)5 X% W' `5 m4 X. X, R" ~5 `
freq_data = abs(fft(QAM_rx_data));
) @' H1 D0 u2 U L = length(freq_data)/2;0 V* M. S3 S4 E; D3 i- v
plot(dig_x_axis(1: L), freq_data(1: L))- N( q0 G3 ^& }' u3 w" r. V" f: X
title('FFT of Received QAM')
+ |2 {& Q/ C7 c- u axis_temp = axis;1 R# W; A1 U3 e8 B f
. _( V- Y5 m0 W. H2 j- I
subplot(211),' O8 ^! _9 x: w2 p: r, ?0 E
freq_data = abs(fft(QAM_tx_data));
0 |0 \( M% D \; @8 y2 l' ] plot(dig_x_axis(1: L),freq_data(1: L)), axis(axis_temp), _ v" r3 c$ z
title('FFT of Transmitted QAM'); E6 q: V' c6 e, Y3 S
end2 D7 o1 y+ {0 t
( U6 h$ D4 H0 l& i: o, O1 l, R* {
% Plots the QAM Received Signal Constellation
9 S- _4 R- ?6 q- g0 I8 m+ N figure(5), clf, plot(xxx,yyy,'ro'), grid on, axis([-2.5 2.5 -2.5 2.5]), hold on9 [1 d- ?1 e, ~. m* E
' p2 G* @+ @5 v* c
% % Overlay plot of transmitted constellation
+ j6 k3 X5 ^8 [1 t. h- ]& V* B% x_const = [-1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5];
3 d0 b/ T* P z w% y_const = [-1.5 -1.5 -1.5 -1.5 -0.5 -0.5 -0.5 -0.5 0.5 0.5 0.5 0.5 1.5 1.5 1.5 1.5];$ j* `+ z7 w- `, G: R, A( S1 W
% plot(x_const, y_const, 'b*')( L8 J# j- `5 E; Z1 w' a3 a
: J+ m) [ _5 P4 z: o! H
% Overlay of constellation boundarys: E: K& Z2 c4 e( ^( G; u1 z
x1 = [-2 -2]; x2 = [-1 -1]; x3 = [0 0]; x4 = [1 1]; x5 = [2 2]; x6 = [-2 2];- `& m# m7 o4 ]
y1 = [-2 -2]; y2 = [-1 -1]; y3 = [0 0]; y4 = [1 1]; y5 = [2 2]; y6 = [-2 2];
: K3 V& ?3 f8 e$ [. X plot(x1,y6), plot(x2,y6), plot(x3,y6), plot(x4,y6), plot(x5,y6)9 z# R: U i8 u
plot(x6,y1), plot(x6,y2), plot(x6,y3), plot(x6,y4), plot(x6,y5)
% P! M9 @ `: h3 M# x& a7 E- d0 O: }# a
hold off5 N& T0 E2 o, [4 F+ \
title('16-QAM Received Signal Constellation and Decision Boundarys')
) u7 ^, ]8 C5 s8 G/ _% S! X / }5 W& V% V" X
binary_err_bits_QAM = 0;; `8 Z0 L6 {) E" P: p" w
for i = 1:length(data_in)1 M/ P5 N8 }) \, n$ t& [6 {
err = abs(data_in(i)-QAM_data_out(i));7 S6 e& G- z4 m4 i! |& B
if err > 02 H4 k% G1 ^) ~6 z$ ]$ |
binary_err_bits_QAM = binary_err_bits_QAM + 1;; N9 _5 l0 [# G n
end3 W( m) |& C1 g; |
end
# N4 {, l4 G: Y8 q9 @ BER_QAM = 100 * binary_err_bits_QAM/data_length;: |! F e4 U* k7 [
end7 G( n( f. d6 f$ K
) ] V/ X/ f0 C
figure(6), clf
* y ?. j3 V9 R5 m+ e7 Zif channel_on == 1) ]5 G# {' t* ^, I
subplot(211), plot(W/(2*pi),mag),title('Channel Magnitude Response')
/ z B1 `* D. Q, h xlabel('Digital Frequency'),ylabel('Magnitude in dB')8 L* y* F+ b$ }+ x( w/ I
subplot(212), plot(W/(2*pi),phase),title('Channel Phase Response')5 A, t8 d; O9 l
xlabel('Digital Frequency'),ylabel('Phase in Degrees')& _4 i8 v+ N6 {! Q
else- d: G1 [: G( p: t
title('Channel is turned off - No frequency response to plot')$ r2 `% ~: E) `9 g; I. g2 ~( v- Y
end, I5 ^( P8 J5 U$ m7 x2 L
/ z- \# ]& Q/ x1 _' N" ]% Compare output to input and count errors
2 Q5 D" B/ z [% \. j5 I) pbinary_err_bits_OFDM = 0;
' w1 L! ~; p: P: A0 x' Afor i = 1:length(data_in)/ p9 v& W2 t( j, B9 F4 O: d
err = abs(data_in(i)-output(i));% y+ g( f, I) l
if err > 0# l7 h5 ]- }% o7 b% _
binary_err_bits_OFDM = binary_err_bits_OFDM +1;
* }/ V: H1 ~ R. _7 b, ~ end
; D0 p+ |( e2 {0 ]% `/ Z( ?: tend
' V7 `8 \* R; H! B$ @& IBER_OFDM = 100 * binary_err_bits_OFDM/data_length;4 @, C4 [% S% R3 \+ t! l* Z
disp(strcat('OFDM: BER=', num2str(BER_OFDM,3), ' %'))5 Q( m$ b+ Q3 w7 D- K
disp(strcat(' Number of error bits=', num2str(binary_err_bits_OFDM)))
! X( I( R6 }6 `6 W# r! R( B7 R4 y# i
if (do_QAM == 1)( H8 n1 [3 B N) u
disp(strcat('QAM: BER=', num2str(BER_QAM,3), ' %'))
- ^4 q+ Y& T! Q6 x, Y! x U disp(strcat(' Number of error bits=', num2str(binary_err_bits_QAM))) j4 v+ }! K( d* C# q8 {* l& K, _7 `
end$ f5 w3 i2 y1 ]" a9 y. c6 y! k
! r& B' a0 k0 @0 U( h$ d8 T% Display text file before and after modulation- E- _# [9 l5 `' q
if (input_type == 2) & (file_input_type == 2)0 g" ], i0 d" i0 a8 R8 s
original_text_file = char(data_samples')1 H: W9 {" g" d2 V9 {" v* \. a
if do_QAM ==1+ w6 `1 `, Y/ V
edit QAM_text_out.txt
5 i( C, F0 F d7 B end m! j% F/ g7 e/ D9 ^, l
edit OFDM_text_out.txt: b! r X; I3 [) q. \
end w% W% t5 g* o% H" f
5 n0 U/ @( R) `! ]% t' \$ h% Listen to sounds a! w O/ n4 T" ]( p
if (input_type == 2) & (file_input_type == 3)4 {: P' T$ c4 O7 {
do_again = '1';, A+ S( Q- P7 _2 q8 e* J5 |
while ( ~(isempty(do_again)) )
# r7 e% N6 i+ g# t) ~" { disp(' ')
- S+ y2 x$ G2 t# a' ] disp('Press any key to hear the original sound'), pause
) ]/ v7 Z, U& T3 p0 p! H2 F5 F sound(data_samples,11025)0 ~1 i# n) r" D8 t( j- i1 e" |
disp('Press any key to hear the sound after OFDM transmission'), pause# o( |/ `# ?5 `, @: r. Y
sound(output_samples,11025), j! N* A( `& t( [; @
if do_QAM == 1, ~3 I" e+ L; R1 [- \3 p
disp('Press any key to hear the sound after QAM transmission'), pause
2 i' J$ G& w& _! F/ t* \# ~$ z sound(QAM_output_samples,11025)8 Z1 E4 A0 M0 c
end
4 k U9 i l' o/ g( s% m do_again = '';2 \' Q6 C2 I3 W- `
do_again = input('Enter "1" to hear the sounds again or press "Return" to end ', 's');
, A7 O5 j: I" f5 m4 ^! l end
b6 v$ D) ?" m: U5 Zend
3 y+ a; @4 M" ?7 p o4 I. p
) V3 Z7 |: G2 D+ W1 }. |8 V* [6 b2 I& Y3 C. `* v- l& G
|
|