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