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