找回密码
 注册
关于网站域名变更的通知
查看: 370|回复: 2
打印 上一主题 下一主题

OFDM的仿真程序。

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2020-4-20 14:40 | 只看该作者 回帖奖励 |正序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
本帖最后由 House 于 2020-4-21 14:03 编辑 ) s1 j2 H4 y& D/ _$ y. \

" k0 T# c3 h( W1 iOFDM的仿真程序,并且包括详细的WORD说明。有误码率和各种性能分析的做图程序。有需要的可以看看。9 p0 @. C% c* e7 ?( B7 Z& ^% T& Y

1 A3 p0 z- Z3 O%a_run_design.m文件# c  E4 _& x; R, b4 ~
setup                                %系统设置) \. z7 X' U8 b+ v
QAM                                %QAM调制方式
* _0 Q7 [( U0 p2 y1 xOFDM                                %OFDM调制方式* O. o0 A3 J& \% @/ W: Z# E# P
Analysis                                %QAM和OFDM两种方式对比4 k0 \# b' _/ b. [/ ?
% I/ R7 g  \/ j3 Y+ r% S- l# P% I' u# L
1setup            %初始化部分
  w) o7 {$ p" t6 V" |3 _2 v% setup6 Q# `0 E2 a. A
disp(' '), disp('------------------------------------------------------------')1 q1 {" _7 J9 i  z0 B5 q
disp('Simulation Setup')
, Z( y& C/ `- c) m
7 O5 x! w3 S* Z$ C% OFDM Setup -----------------------------------------------------------
9 f, N1 D1 ]& ^7 ~fft_size = 128                % should be a power of 2 for fast computation! y) F) F5 {& m7 j2 n
                                % more points = more time domain samples (smoother & more cycles)( R# ]  @( f# q- |  U( k2 K
num_carriers = 32        % should be <= fft_size/4
; ~7 h4 h% e- ^4 U; A7 v9 x                                % number of carriers used for each data chunk
/ p8 S1 z% L- i. B5 z% R% new var - denotes even spacing or variations of carriers among fft points% t  n5 f; [  L" z' a: z
input_type = 2;5 Q; ~1 n; v1 U1 k# a
% 1 = test input
+ d( x' b$ z3 ?$ y" B        test_input_type = 1;
+ C9 j1 q9 C' |* X) K        % 1 = bit specified (binary)- [3 c) O; `; U- ~# u4 J3 m, t
                binary_data = [0 1 0 1 0 1 0 1];
* m5 [  }, W( s) A  |# b& Y        % 2 = random data stream (samples in the range of 0-255)
$ z4 l- m$ v6 U, x. V% {                num_symbols = 9;& i7 k; V  M# n8 M
        % 3 = sinusoidal
* i1 C: G- \. z/ X; A4 Z                frequency = 2;
3 b: c: L& K$ p) U: }: i2 F  y                num_samples = 50;
7 i% v& [0 K* g. ~/ b5 H% 2 = external file input& J5 f* e( b. s6 s
        file_name = 'shortest.wav';                % Name of input file
4 \  z3 @. t/ j5 B" ^; t& ?5 V        file_input_type = 3;+ ]; q. [) u# v& i. |4 ^" F
                % 1 = binary (not implemented), k% s/ a0 n0 V2 W1 B
                % 2 = text                                        % Demo file:  'text.txt'2 Y. A+ U# m" m, U) Z. L. D; s2 E
                % 3 = sound                                % Demo files: 'shortest.wav' & 'shorter.wav'
$ V% H6 b3 [9 s% W                % 4 = image (not implemented)
4 o; x# ?' |0 [9 ~- I) X) |+ A5 R. G9 `# O
% QAM Setup ------------------------------------------------------------
6 y9 f! G9 f9 v7 }$ k5 B4 C: hdo_QAM = 1;                % (1=on, 0=off)
, k1 A& \! ?2 c& zQAM_periods = 10;        % defines the number of periods per QAM Symbos (1=2*pi)
  r: X7 U5 @  [1 p
" n+ l  N, Q4 f5 A) F6 i/ i% Channel Simulation Parameters --------------------------------------------2 s2 N9 ^$ {( ~! ?/ k3 b
channel_on = 1;                % 1=on, 0=off
5 c4 Y6 [  Z. H& a7 S! tclip_level = 1.0;                % 0.0 - 1.0 (0-100%)
7 U) J( T6 Q3 q8 r( t7 C        % Max magnitude of the signal is 'clip_level' times the full magnitude of the signal3 f, M! }1 i; j% y
noise_level = 0.0;        % 0.0 - 1.0 (0-100%)' A' s. |2 b3 J8 h: A8 t
        % Magnitude of noise is 'noise_level' times the magnitude of the signal
( _1 q/ X4 S7 q- s' x5 g% Multipath Channel Simulation
9 f; B+ Z7 x( N( g5 |        % Good defaults when fft_size = 128 and num_carriers = 32:3 k" J1 K# V1 j4 z3 ^' X1 m
        %        d1=6; a1=0.30; d2=10; a2=0.25
$ r. L( F: B  s& Z( E8 c        d1 = 6;                % delay in units
! k# @6 u$ e0 c% _, `        a1 = 0.30;         % attenuation factor - multipath signal is x% of size or original signal( _# I: h- F6 w" x2 P. C, |4 v* q
        d2 = 10;                % delay for second multipath signal
# T$ l2 m- ]5 v; x( v9 n: s9 m        a2 = 0.25;        % attenuation factor for second multipath signal/ }) X" z# A/ t; b7 N! {. k
( `* s. O: j6 B9 W6 r" t
9 O% ^2 r  ~0 r# R
% ****************** TEST INPUT SETUP - DO NOT MODIFY **************************, d0 f6 c5 \; @/ k, s$ @
if input_type == 1
9 O0 ^9 R7 o, d5 \        if test_input_type == 1# T0 B) l5 S( X1 @/ A. y# J
                %specify BINARY input bit-by-bit% w7 }/ Q" Y- A+ k
                data_in = binary_data;
4 `& ?8 T# P% _+ Z  U6 T        end; ]3 B2 K& M# v! N' ~# \0 E
        if test_input_type == 2
2 {* q4 A6 T9 |9 N$ y! I                %random input defined by parameters7 N7 w7 W1 _# e' }9 ~
                num_levels = 255;                %number of possible levels of a symbol
1 R' O+ D; `" i- \: g  `                                                %must be integer between 1-255
1 Q: {3 D9 ^3 }+ z: d0 G                data_samples = round(rand(1,num_symbols)*(num_levels-1));) {5 w7 d/ _$ {/ d4 z
                data_in = zeros(1,8*length(data_samples));
- F  ^( d( L$ S- N" u) G( B1 j0 z                for i = 1:length(data_samples)' v- k- K+ ?% _& L
                        data_in(1 + (i-1)*8: (i-1)*8 + 8) = eight2bin(data_samples(i));
) F" x7 u3 E; ~. m% o2 ^7 }                end! n' ]6 c5 P$ P; J  V2 V
        end
7 d$ q. j4 T# K" B# j        if test_input_type == 3/ K. L7 R4 |9 [6 y8 u9 u
                %data stream represents sine wave samples
  A; X4 O  t! ~& `5 {& M. e                t = linspace(0,1,num_symbols);        %evenly space number of samples$ H* Z: T1 \- \6 {2 M8 m
                %take 8-bit samples of sine wave4 @! [% Q2 Z# v+ Z" m3 D6 N' k5 r
                data_samples = round(127.5*sin(frequency*2*pi*t) +127.5);
+ K$ x, {3 o2 L8 }                data_in = zeros(1,8*length(data_samples));
4 j2 v( a# C- Z7 e8 p9 q                for i = 1:length(data_samples)* q3 l0 S$ W" t6 z
                        data_in(1 + (i-1)*8: (i-1)*8 + 8) = eight2bin(data_samples(i));
. N3 X9 O# c& z6 L                end; a; W  L. C  M
        end- U+ }0 U5 O% E) F2 u+ f; A
end
9 m+ w* G+ E8 c( d8 G& W* k4 `
already_made_noise = 0;        % initialization (don't change)
# ?" `1 M4 ?  a3 D* Z. a% X$ T- n2 v0 f  e' t+ h
2、QAM.m. J$ ]+ }. P; m; X3 W$ Y
% QAM.m compares OFDM (multicarrier) to multi-level QAM (single carrier)$ v$ v/ [8 {- F$ w* Y: Y
% when they transmit the same # of bits in a given time period
" f; `' q( s4 I6 `
( S0 G3 e7 c; o8 F8 _: A8 m2 F% \read                % read data for QAM - does not affect OFDM- c9 _% X( W+ j0 ^$ r
data_in_pol = bin2pol(data_in);                % Converts binary data to polar data
- m( h) u. b. y  X  Y) a0 @5 \1 P! [) P- Y/ |2 Q& G
% check to see if num_carriers is a power of 2
# {( G& Z& B2 y. e5 G/ K& Xis_pow_2 = num_carriers;
) y' j$ f9 }0 @' L( e4 s7 Ytemp_do_QAM = 0;, I# e- ~4 k) m3 U! l% F/ Q+ d- c! V
if is_pow_2 ~= 25 ?& w8 T2 ]4 z0 ~
        while temp_do_QAM == 0
, N- T' J& a: ?                temp_do_QAM = rem(is_pow_2,2);
+ H' k8 O# k2 [; `; F6 D+ T% n                is_pow_2 = is_pow_2/2;
8 E6 q' A! I+ F6 o                if is_pow_2 == 26 U: F, t( z. g
                        temp_do_QAM = -99;                % it is a power of 2 -> can do QAM
/ J/ \1 a9 t1 V" A/ J! @                end
% `4 V7 K0 U, A7 s8 p% b        end
8 b6 B% w; R$ N9 delse0 C4 [' u  u  K# |& [+ B5 ?
        temp_do_QAM = -99;        % 2 is a power of 2
$ j. a6 j5 P+ n2 ^) L5 f2 f' Tend
+ J/ D3 L8 z! uif temp_do_QAM ~= -99/ r! g6 k5 v4 m2 e) v% q: a; j& d: {
        do_QAM = 0;        % don't do it if it's not possible. v, `% a9 M$ {2 _& ^  ^
        disp(' '),disp('ERROR: Cannot run QAM because num_carriers is not valid.')% p/ U' f$ y: P+ G4 c) ]
        disp('       Please see "setup.m" for details.')
6 Z: c6 i' X' k4 Eend  L! e# T6 i* v5 y8 V

; \) J# ?6 i6 J2 `* g9 A/ J( G4 b7 S; t0 b, I
if do_QAM == 1! G2 b, `2 Y/ D& g% i
        tic        % Start stopwatch to calculate how long QAM simulation takes
' Q1 y4 M( a. q* u! t+ n        
" |  {4 n" u2 \$ S% P) @- K2 @% D$ Q        disp(' '), disp('------------------------------------------------------------')
, {  S+ k6 N9 Z/ U2 x        disp('QAM simulation'), disp('Transmitting')- S# R9 V* v; O& d# g3 q+ S
        4 p* @; v& Q) o
        % Pad with zeros so data can be divided evenly5 f& v# s+ _; a; W
        data_length = length(data_in_pol);
; j( _9 T8 I# _  U' d! E3 d! I        r = rem(data_length,num_carriers);
& ], O$ }, {) T3 v- D. a        if r ~= 0  M+ ^7 A$ t4 J* w% e
                for i = 1:num_carriers-r
0 l0 w6 [) F$ J, S# Z0 i                        data_in_pol(data_length+i) = 0;        %pad input with zeros to complete last data set
7 t8 q9 T* _- X0 {- j8 ^                end                                                        %speed improve possible
3 V  F0 @, \( V( F; y/ [8 l        end  o& r+ w; O( f. o( C! G
        data_length = length(data_in_pol);                %update after padding
4 z7 X+ H" q' ?) s9 b% w, ^8 z& T        & P4 Z) A3 z0 s% ]" I5 s- n9 i
        num_OFDM_symbols = ceil(data_length / (2*num_carriers));9 [* _( h8 h9 j9 a7 U% S
        % num QAM symbols that represent equal amount of data to one OFDM symbol
5 B/ y2 d, Y: Z$ x( D2 _        num_QAM_symbols = num_carriers / 2;$ Q( A/ n, g$ V+ q  q9 t, M
        % num samples per QAM symbol
2 b# t9 O# J$ [/ e' r        num_symbol_samples = fft_size / num_QAM_symbols;/ L9 e; ^& j0 f& A6 b: c# [
        2 X/ v$ @" I9 t* ^0 b% k
        % convert polar data [-1, 1] to 4 level data [-3, -1, 1, 3]
( p( l* A/ o! E/ D$ b+ H# y        data_in_4 = zeros(1,data_length/2);- r8 g8 X: k+ |6 K5 _" H- C
        for i = 1:2:data_length
9 V8 B) n# ^( B8 z1 ~/ k                data_in_4(i - (i-1)/2) = data_in_pol(i)*2 + data_in_pol(i+1);
: _0 y8 L# \  H        end- T2 B$ s7 w' e. i) A: W2 {
        
" h- ~$ G6 }& Q) l8 t        % define sample points between 0 and 2*pi6 Q$ I2 S0 l' c( _; P
        ts = linspace(0, 2*pi*QAM_periods, num_symbol_samples+1);5 U( ?2 u2 i4 n) D
        . ?4 v1 S+ w8 f3 {
        % Generate 16-QAM data
  G0 Z* Z" g$ c! i2 C. F6 h        % total length of 16-QAM transmission
4 B" \0 e& s5 _! j        tx_length = num_OFDM_symbols * num_QAM_symbols * num_symbol_samples;
8 k; a9 ], b7 K* u' Z# d        QAM_tx_data = zeros(1,tx_length);# W" s( x' \* s3 M  w
        for i = 1:2:data_length/2* V2 F7 w! g4 y- F7 L5 d7 m
                for k = 1:num_symbol_samples- l. x! j3 N- |, h/ w6 u/ ?
                        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));1 \( c$ }* J& P/ R, L. r- b  u
                end
" o% r7 v3 X/ L, [  v7 J6 v) r7 \        end+ k. S( F# ?/ R8 ?
        1 S  L1 ^1 `3 Z6 c) k
        % Do channel simulation on QAM data; }% ?7 B8 f$ y" e
        xmit = QAM_tx_data;                % ch uses 'xmit' data and returns 'recv'( P2 b, X3 ?( q- j; D
        ch+ I' W7 a# R9 D% e0 _! l
        QAM_rx_data = recv;                % save QAM data after channel
* R0 a* t9 @: ~- p7 ~1 j* K        clear recv                                % remove 'recv' so it won't inteRFere with OFDM
2 a) T7 ]6 Q5 ^. L* b3 a        clear xmit                                % remove 'xmit' so it won't interfere with OFDM; n1 S0 F2 ~; d9 [: p" ^
        
& f# g! o( a) i- V) v        disp('Receiving')                        % Recover Binary data (Decode QAM)
9 h1 N6 s( Y5 Y  f/ ?        cos_temp = zeros(1,num_symbol_samples);                %
, h3 X6 o* C, n- b2 e; G        sin_temp = cos_temp;                                        %6 D( J5 @0 S& J4 o! v3 {
        xxx = zeros(1,data_length/4);                                % Initialize to zeros for speed; r9 _: G0 J, Y0 I; e1 F
        yyy = xxx;                                                                %$ p; w2 a0 m7 x) `' u  \2 g
        QAM_data_out_4 = zeros(1,data_length/2);                %
% y, i2 m0 j& a9 v8 r! F+ I( l        ( c$ N% S0 Z7 O; o  m! v+ M4 k2 J; o
        for i = 1:2:data_length/2        % "cheating"
5 v) N2 y! G/ E3 o: z& p                for k = 1:num_symbol_samples
4 O& o1 @* e' |4 @5 E1 X                        % multiply by carriers to produce high frequency term and original data
% k7 u5 h; f' w4 I6 N& [                        cos_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) * cos(ts(k));/ T* P8 B0 |6 Q8 m( P% z
                        sin_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) * sin(ts(k));
" y2 Q5 }2 F, j3 Z/ o1 c, B# Q8 M                end
4 J" }% _- M0 u9 L2 R                % LPF and decide - we will do very simple LPF by averaging
; z* ~/ {. O! a9 C7 X) A+ C, R( c                xxx(1+(i-1)/2) = mean(cos_temp);
) c: e; F1 F9 w                yyy(1+(i-1)/2) = mean(sin_temp);
9 f7 l0 R3 Q+ q( U$ t9 b) ?: X                % Reconstruct data in serial form  c& _, ?, F) Z
                QAM_data_out_4(i) = xxx(1+(i-1)/2);
/ p& L  d/ K+ o. U( v' v                QAM_data_out_4(i+1) = yyy(1+(i-1)/2);/ p/ s+ Z9 V; W1 |# d( g
        end
7 ]  _( I$ _, t. M. N: {        . _2 `. n6 \. w. C& G! y8 Y
        % Make decision between [-3, -1, 1, 3]' T! w0 h8 F! d2 A8 Y
        for i = 1:data_length/2( h  @' f! U+ z  N0 a9 F  Q$ n0 Z
                if QAM_data_out_4(i) >= 1, QAM_data_out_4(i) = 3;
1 f1 Z) w6 F9 Y" @  H7 h                elseif QAM_data_out_4(i) >= 0, QAM_data_out_4(i) = 1;  {2 j/ Q/ c6 @/ m' y. ?$ {) F
                elseif QAM_data_out_4(i) >= -1, QAM_data_out_4(i) = -1;. S% F% F# Q) }! J8 }1 R
                else QAM_data_out_4(i) = -3;
0 X% T8 S; H) M8 s) j  W2 q                end. E/ M' w- t5 x7 B& y4 _& {7 H% V
        end
, U! W- L. `/ ]5 a3 P3 P% j$ |! @        4 h, \" L% Z& S$ S' n! c
        % Convert 4 level data [-3, -1, 1, 3] back to polar data [-1, 1]
0 ?7 ^" n- y3 x! T: Q+ N( Q        QAM_data_out_pol = zeros(1,data_length);        % "cheating"
- r& O# O7 U! [& l0 @7 j        for i = 1:2:data_length
7 M" O  l' ^1 ?" m2 i' M' F# ~                switch QAM_data_out_4(1 + (i-1)/2)6 U7 Q+ t7 S: g8 c3 H$ M
                        case -3- M# P% L: z$ h8 R
                                QAM_data_out_pol(i) = -1;- {8 ~# b  l" [
                                QAM_data_out_pol(i+1) = -1;
( `& G% B; S& z/ g) M  E! ^2 r* @# {                        case -1
4 y3 _1 f( q( Z$ Y3 [                                QAM_data_out_pol(i) = -1;) }8 I) a8 o  A& W
                                QAM_data_out_pol(i+1) = 1;
! u8 H/ d) h9 m! G% N, x                        case 1
( x( n) {! @0 J* R+ \                                QAM_data_out_pol(i) = 1;
& Q5 t7 s* ^: |9 H2 B1 M% N- Z                                QAM_data_out_pol(i+1) = -1;- G5 K' ?" h5 I2 B
                        case 3
  j0 K. R$ [# S' o' x5 M+ S                                QAM_data_out_pol(i) = 1;
) @# |" E+ Q: `6 s& V" F; m/ R# V& I                                QAM_data_out_pol(i+1) = 1;, M& {. e# h* I( k% W" I
                        otherwise
7 j3 ~. R- K9 h/ f' u7 p                                disp('Error detected in switch statment - This should not be happening.');
" ^1 x- n% M+ W+ l1 o                end
* z* ^- ?  ^, o1 s; J& j        end$ b4 ?# }' f% f0 c: q
        QAM_data_out = pol2bin(QAM_data_out_pol);        % convert back to binary3 ?9 c; Q/ ?" I' f
        
. h2 U2 h' c0 ~# ~$ d5 Y        % Stop stopwatch to calculate how long QAM simulation takes
" K& Q; B$ j9 |, d        QAM_simulation_time = toc;
6 e3 y. ?9 j0 P        if QAM_simulation_time > 60
! }* _3 n8 Z/ z2 F8 E/ g                disp(strcat('Time for QAM simulation=', num2str(QAM_simulation_time/60), ' minutes.'));
1 Q. ]: }3 {0 T, s  a' F4 U& |        else) r! @. J& X  {
                disp(strcat('Time for QAM simulation=', num2str(QAM_simulation_time), ' seconds.'));  D5 T5 O0 x2 o, Z7 D6 Y
        end
$ k; F) E8 n5 x# h+ Gend
5 w- a, y0 t5 f( h8 p8 d4 ?0 X4 d
: i; o. m: S- d: X) ?+ e. J3 % Run OFDM simulation
$ j- A+ y- i+ I. k, s+ ~tic        % Start stopwatch to calculate how long QAM simulation takes
, b* {6 E. Z! E2 Z; _# Sdisp(' '),disp('------------------------------------------------------------')& V; @; b7 g# Z! ]4 q9 f0 {& h% {  T) `
disp('OFDM Simulation'). h% v; S9 w& {1 A7 Y
tx         # c8 _, X+ w$ f' h' L( b
ch, W& s" t# I  M$ t! ^
rx" U/ V8 b6 K  t. y4 h% S( \
% Stop stopwatch to calculate how long QAM simulation takes
8 @4 k. k! ^) y! ^OFDM_simulation_time = toc;
' x. H  J6 a! Z+ H  @% e! L  Vif OFDM_simulation_time > 60% I* \8 m4 O, v5 A
        disp(strcat('Time for OFDM simulation=', num2str(OFDM_simulation_time/60), ' minutes.'));  X7 [5 R/ @! X9 v1 V8 j
else! @  k' C3 L1 R0 S
        disp(strcat('Time for OFDM simulation=', num2str(OFDM_simulation_time), ' seconds.'));, {: ~4 u  Q/ S. `4 {
end
" q1 t$ a2 C: y6 E: @! ?
0 c- z9 w8 v& I3.1发送6 e- N& V. w5 S1 D& d6 x
% tx
- b& ]4 W! \! `& T6 N( C( Z; jdisp('Transmitting')
0 U& b" }  H/ `4 t: d) Cread                            %read original data- w; o# b0 w+ ^- Q/ X
data_in_pol = bin2pol(data_in);           % Converts binary data to polar data( z9 W3 ?& u  f
tx_chunk                        %convert polar data into chunks.      - x8 Z( ~( J; S% z

0 E" y6 C3 x+ Q3 l5 r. V% perform ifft to create time domain waveform representing data) h! Q# \4 Z& |" P( ?5 X$ F
td_sets = zeros(num_chunks,fft_size);
: H3 V4 Q5 Y4 F, l3 D2 R! _for i = 1:num_chunks
, J- s. B7 v" |* u. ]8 A% V        td_sets(i,1:fft_size) = real(ifft(spaced_chunks(i,1:fft_size)));
' ^, ]9 _( j; \- p. ^end- Z7 |. S8 k& Z* \, \1 p. c: Z
! ~# g0 x% j/ w% c8 B
tx_dechunk      % Construct signal to transmit by placing time domain sets in series( _7 l* t+ k8 V
. }* z% @/ z5 F/ s& C' y
3.1.1 % tx_chunk    %双极性数组转化成OFDM字符串
; h2 t, y- F, `  j  H  q. ldata_length = length(data_in_pol)                                    %number of symbols in original input& W0 p, B2 a  M
num_carriers
& A4 b4 F( K0 N8 K/ Fnum_chunks = ceil(data_length/(2*num_carriers))        %2 data on each carrier (real and imaginary)
- C! O$ f# @* }2 {9 s! tr = rem(data_length,2*num_carriers)
6 T4 O" C. o4 Jif r ~= 0
* y; S. b, |* i- \        for i = 1:num_carriers*2-r/ i# [2 `! j1 k* b2 [+ b# `
                data_in_pol(data_length+i) = 0;        %pad input with zeros to complete last data set
. g3 S! H  F2 o  D        end                                                        %speed improve possible) L" K0 M1 h) x3 d5 x/ }! j
end
" a4 |2 [  c! m) _9 x5 v$ n8 e4 h5 V- \  M2 l1 T
% break data into chunks7 V0 N2 ^/ j9 G
chunks = zeros(num_chunks,num_carriers);        % for speed
1 N5 R* P$ C* X( X+ Ufor i = 1:num_chunks
  F7 `8 l% w8 N' p( S        % *********************chunk done
9 E6 `8 N& \; Y: h0 Y- U4 P        for k = 1:num_carriers
# ~4 Y7 U+ j6 a- A) x                chunks(i,k) = data_in_pol(2*num_carriers*(i-1)+k) + data_in_pol(2*num_carriers*(i-1)+k+num_carriers)*j;1 m! y7 r# `7 V
        end) b, [; g! @0 n# b
end4 ~0 Z; u! I6 i: I) I4 ^# J( K

1 _. ~, r0 I- p* S9 R7 lchunks+ A0 I+ k  W$ i. e+ {/ N8 e& x+ |
% Padding chunks with zeros so num_carriers and fft_size are compatible' y# x; V1 l) {$ o0 W
% Once compatible, further spacing is simplified
3 E/ S+ h8 {) y% g9 [num_desired_carriers = num_carriers;5 w7 V% T  V3 e- G3 H
num_zeros = 0;
* b( r: F- q& k" U+ Kthinking = 1;& ~6 ?2 D1 G% U0 h' R. d2 e% }
while thinking == 1 % Continue if num_carriers and fft_size are not compatible
& Z% Z8 D3 M: c1 x  T, H( N7 C        if rem(fft_size/2,num_desired_carriers) == 0
2 V& t( z) H3 j& C9 d                thinking = 0;* D' J' ?; G( C, a% W7 V4 C  S
        else% S4 L5 Z* `2 A8 g- L( E" [% s. K# C
                num_desired_carriers = num_desired_carriers + 1;( V8 r* `/ C: k' v; j' X
                num_zeros = num_zeros + 1;
0 y4 y# m( ]+ M$ w        end$ v+ [0 n6 l# t
end1 l/ ~4 y! B! Y3 a8 d- j( l

0 ^; V7 p9 ^$ |: g& P$ ypadded_chunks = zeros(num_chunks,num_carriers + num_zeros); % for speed% L% {; Y% [$ i  y' E# i! U
padded_chunks(1:num_chunks,num_zeros + 1:num_carriers + num_zeros) = chunks;
  i% I' I2 h, Y+ p/ v
* q* X# g/ w5 ?; ~$ c1 l/ a. \9 q5 {) S; e1 M* w! y8 @  H
%compute zeros_between
' y! ^& A" q' m- J5 Bzeros_between = ((fft_size/2) - (num_carriers + num_zeros))/(num_carriers + num_zeros);
. g" ~0 j# O, g6 }4 V* d, y- B7 R- j+ g3 A3 s
spaced_chunks = zeros(num_chunks,fft_size); % for speed - extra room for folding later$ x7 a3 P9 }" X7 }' g# I
%add zeros_between/ s, O% }: h% W6 T# e
i = 1;
) J/ e/ Q0 D' B8 D. G, [' P3 ffor k = zeros_between +1:zeros_between +1:fft_size/2
8 P% E$ ~% x# i5 r% j        spaced_chunks(1:num_chunks,k) = padded_chunks(1:num_chunks,i);  B4 t8 q6 \$ i. \- n' c. O, G- ]
        i = i+1;
8 f; _4 G- ]! {( K, |5 {: lend
1 Q  [0 m/ b5 r: e  W8 C6 [/ ~
% folding data to produce an odd function for ifft input
5 @' W% w2 i# R3 e5 yfor i = 1:num_chunks6 `3 z/ _# a: V9 l9 {+ ?! G8 _* K- v( V2 L
        % Note: index = 1 is actually DC freq for ifft -> it does not get copied over y-axis% D" [% O1 J/ W! x
        spaced_chunks(i,fft_size:-1:fft_size/2+2) = conj(spaced_chunks(i,2:fft_size/2));
( U" f6 m, ]: Gend! `7 E* |% Z" s3 g+ ^

" o( E5 B& A, i* _$ d( h+ a7 }3.1.2 tx_dechunk# R" a: [- z, s8 S/ v5 \( N
% tx_dechunk
: L( u9 ~# t; M$ a: d. ~% v; A' ^5 l  [+ l- }: t) X
% Construct signal to transmit by placing time domain sets in series
5 U& A+ ~0 @5 r7 E+ Mxmit = zeros(1,num_chunks*fft_size);
) }/ e- K0 J; D) B1 y5 ]for i = 1:num_chunks
# b: ^3 ~! s1 J, t2 W1 G        for k = 1:fft_size
9 U3 \" E# h0 q5 V# U% {                xmit(k + (i-1)*fft_size) = td_sets(i,k);
5 o+ S# ], ~  `1 b; Y" ^, P; e        end7 O9 y8 o6 e& ?+ h* O% }0 K
end
0 U4 z, C) Y. O  q' }
( g/ E9 w- U. f; n3.2 %ch.m
2 Y' Q9 {$ B) i; R! Z% ch
+ g$ G' ~2 `3 X6 W. i( drecv = xmit;        % channel is applied to recv, don't modify transmitted data
( t* f. k# _9 `9 g. wif channel_on == 1
5 m! _+ g# A/ Q        disp('Simulating Channel')* ?+ H" ~- I3 V0 k" f
        norm_factor = max(abs(recv));        % Normalize all data before applying: X7 l$ y8 D5 P8 d' N3 F; J+ X6 J! [) R
        recv = (1/norm_factor) * recv;        % channel for a fair comparison
& G( v- t( J; w, d        ch_clipping                     %clipp data
! y& K1 x+ `: G& M3 C! V8 i/ o        ch_multipath                    %
5 p* D0 a. L: L8 W        ch_noise                        %! d( @; d$ X3 k9 O3 z4 u
        recv = norm_factor * recv;                % Restore data magnitude for proper decoding
9 i+ z9 Z) o" ^end# h  k. [$ H2 t4 b, Z" h4 _
' |! G& _5 F/ ^: L# [
3.2.1 %ch_clipping.m2 Y8 M: T/ o( Z$ l) Y- @) a
% ch_clipping
3 Z( q1 ?( X5 n3 `6 ]0 ?0 H3 \for i = 1:length(recv)% n+ y5 Y7 Z4 ]9 e1 K- j! k
        if recv(i) > clip_level
/ ]! q3 K) g- G6 X% A/ J                recv(i) = clip_level;0 T/ k8 O! t" y2 k) n
        end8 A2 ~- \- D2 l+ f0 E- i3 \2 O2 u
        if recv(i) < -clip_level
; i! {. h6 ^# x, u9 K0 m                recv(i) = -clip_level;
! L! M2 ?" K8 |: V- k        end
# {. U  b) m! V) z# ~. K' xend* \( O, Y( L  e- j

3 c: v  D, g% S' [; o3.2.2 % ch_multipath                  % 产生多经的方法
4 {5 R3 R: ?& g( \- Z, w8 Ucopy1=zeros(size(recv));
! R! Q- Q* K' n5 @9 O& nfor i=1+d1: length(recv)
. p' s* [. J& X# s: R! U        copy1(i)=a1*recv(i-d1);
9 F/ T' }5 p$ Oend- r' a. y% j4 F# c0 e1 Q3 L( V
copy2=zeros(size(recv));$ u. g  n( g1 R0 g7 `& @( w: M7 `
for i=1+d2: length(recv)
. X, t1 p* d6 x; j! n        copy2(i)=a2*recv(i-d2);  F/ l9 V3 S2 L  O& u. b
end
: y2 T& P$ R: y8 t( K% arecv=recv+copy1+copy2;
1 X; P3 R/ b! d3 T9 d8 v# [( r/ z  y* x  q8 p& }- g! S
3.2.3、%ch_noise                     %施加信道噪声
$ d  |  Y& N4 L0 @+ @% ch_noise (operate on recv)
4 T0 K9 I4 A) E# o4 x: ^. w! {% random noise defined by noise_level amplitude/ s. O& B7 r7 \5 B
if already_made_noise == 0        % only generate once and use for both QAM and OFDM
% L8 J+ Q- b  Z/ w* m, v        noise = (rand(1,length(recv))-0.5)*2*noise_level;
% x3 u& l/ r6 }& m& _  N6 M3 W        already_made_noise = 1;
, Q/ ^7 c) n, x( C! @& ]; wend  e! Y8 ?" h2 Y" ^6 B1 y7 A
recv = recv + noise;
4 d1 A- q8 s: R: _; ]+ A2 l% k0 g0 ?$ W
3.3 %rx.m                        %接收程序1 X1 t2 _* P8 D* t+ M2 s
% rx4 r, l$ x& x0 L9 J3 Q
disp('Receiving')+ _, u% k& i7 W# J$ s# f0 u
rx_chunk. d! H0 g( s4 z8 l2 n1 p( H( F

, i6 c. v% G* `3 {7 X$ k% a4 ^% perform fft to recover original data from time domain sets6 f6 F0 H7 g' p8 x$ O! j( t1 s# l
recv_spaced_chunks = zeros(num_chunks,fft_size);
4 }0 v7 g& G: @: C* r/ Yfor i = 1:num_chunks
$ T6 Y3 G7 O7 D; i! [7 n' h        recv_spaced_chunks(i,1: fft_size) = fft(recv_td_sets(i,1: fft_size));" i" O2 k2 ?1 U) a" I9 T" W$ i' ]7 V
        % Note: 'round()' gets rid of small numerical error in Matlab but a threshold will be needed for a practical system
( n: o; V! N% W0 @+ t        % 2001-4-17 -- Got rid of 'round()' to do decoding more intelligently7 m" S7 F( X1 |1 F; Y
end' S2 G6 r4 C- j
rx_dechunk4 o5 ]; D+ i8 o% J3 h
output = pol2bin(output);        % Converts polar to binary: z& D( h- X  ^' T
write
: D  _' d) g8 y0 O* c# t
; f% q1 Z9 Y% a: p5 G7 d3.3.1 %rx_chunk.m
  h1 x# o) V( ?6 c6 K% rx_chunk
- m! ?# @- r+ D& G7 V" _% break received signal into parellel sets for demodulation8 c$ a: M. P' `
recv_td_sets = zeros(num_chunks,fft_size);3 \. S  W5 _, W; P& f. L# C5 s, u
for i = 1:num_chunks1 L( b1 _; n' i4 |
        for k = 1: fft_size8 D* e' c# }9 g2 k" [& u1 N- I+ F* z
                recv_td_sets(i,k) = recv(k + (i-1)*fft_size);7 L) s! H9 J9 Q1 Y+ s
        end
: d' R3 s8 [( ^# _. H! a- oend
8 C4 _( e3 e/ b0 n9 p# Z' ^3 \  @+ o* F2 `
3.3.2 % rx_dechunk             %并串转换
4 C9 c/ Q* F; {& ^% rx_dechunk: G+ l; p# e" h& Z# h# p4 N0 B0 E
% take out zeros_between from recv_spaced_chunks --> recv_padded_chunks
# g; Q9 L# K1 T6 y5 b: B% grecv_padded_chunks = zeros(num_chunks, num_carriers+num_zeros);
6 ?) v: {0 r* b: pi = 1;
+ E: ~3 m! q  U! V& |for k = zeros_between +1:zeros_between +1:fft_size/2
& [; U: s7 ^7 w: U        recv_padded_chunks(1:num_chunks,i) = recv_spaced_chunks(1:num_chunks,k);2 T9 g% y" n' i; ?
        i = i+1;
  A. O6 I- }& z$ r& h" Z4 \end7 S6 h3 R/ e- n( D* V
2 z3 Z! E1 e& t( V5 k3 Z3 H  @
% take out num_zeros from padded chunks --> recv_chunks' p0 p' e+ {) P3 [1 B
recv_chunks = zeros(num_chunks, num_carriers);" U7 Y! l$ x2 _& S7 S& N. V/ L) n
recv_chunks = recv_padded_chunks(1:num_chunks, num_zeros+1:num_carriers+num_zeros);
1 ?& X& R8 Y9 k4 J  \$ }* u2 j# h0 |6 ~3 y5 }9 F  I
% Recover bit stream by placing reconstructed frequency domain data in series
7 M- e: h/ W% G0 I( L9 Qrecv_dechunked = zeros(1, num_chunks*num_carriers);. _) l2 h( {2 p7 P
for i = 1:num_chunks
# Y+ ~+ P" q; A        for k = 1:num_carriers- q! q  i0 t5 E0 z8 I& X
                recv_dechunked(k + (i-1)*num_carriers*2) = real(recv_chunks(i,k));9 W- i+ w1 r" T
                recv_dechunked(k + (i-1)*num_carriers*2 + num_carriers) = imag(recv_chunks(i,k));% |* }" a( H: {1 t: Y7 |$ l
        end# i6 }: c1 |% Z' }
end
( i) D2 y$ Y$ y+ J6 g5 _2 D+ o7 d# ]( D: T, j. t2 z
% take out trailing zeros from output --> output& M% i+ B; {9 I* k( J
output_analog = recv_dechunked(1:data_length);: ^4 s0 m$ e# Q/ e$ c, X8 [
output = sign(output_analog);
4 W# D8 S7 A6 D& T
/ ^) a! U7 K' n3 @' O; S0 n6 k6 c3.3.3 %write               %save received data
- V* O% Y* O! p) P% write, p2 c8 ?$ |1 I9 j& s  o
% ******************TEST OUTPUT*********************************
9 S5 c+ `7 n" O+ Q% I0 _if input_type == 1$ g5 m- A& B) I" x$ g* u, i
        if test_input_type == 1
4 @- H! ]& i$ A/ y                %already binary - do nothing
0 _% M$ M/ ?0 }' w) f6 l        end
5 u+ G1 \1 s: C1 f        
/ \1 w+ h# L% i6 Y  u8 U        if (test_input_type == 2) | (test_input_type == 3)
/ I  j# O: s/ w6 G* T                %random input     OR     sine wave samples( s7 Y# d' T0 q
                output_samples = zeros(1,floor(length(output)/8));  %extra zeros are not original data, F) s# c6 E" p7 N; j9 B+ j
                for i = 1:length(output_samples)  {1 u9 J5 ]6 p$ V2 H
                        output_samples(i) = bin2eight(output(1 + (i-1)*8: (i-1)*8 + 8));$ ~) Y) d3 r4 J
                end1 H& l! ]% q% v( \) U& z. b, }/ \
                if do_QAM == 17 x: v/ D: e# ~/ |6 g! v
                        QAM_output_samples = zeros(1,floor(length(QAM_data_out)/8));0 `: i: [0 _- p
                        for i = 1:length(QAM_output_samples)
1 D, _4 q! R6 l, N/ p                                QAM_output_samples(i) = bin2eight(QAM_data_out(1 + (i-1)*8: (i-1)*8 + 8));
! f* M0 f9 z% f/ h9 r% `7 b* R                        end) ]. z: |, R! N+ t( G/ I! C
                end
! h* a+ s# M9 q( G* y        end
8 o! ?" g0 |5 O* b) F' p- e9 Iend4 n& S4 Y. H$ {' R7 Z+ |

1 ~6 K) |) \$ }% A* `% ******************FILE OUTPUT*********************************
5 y; x( T; u" |) W+ v1 S: @* Z/ \if input_type == 2
" n; A' R& H3 u5 l
* T9 L5 K: r  a6 B7 U$ ?        if file_input_type == 1
0 c1 y3 P5 ]# `/ I: h/ ^/ L                %binary file output - not implemented
2 s6 T; P$ X% c+ R+ v( ^        end
% a: [( K9 l3 A& F$ F
1 o+ \  O  }& ^        if file_input_type == 2* ~* P+ d' [3 ^" i; K: }+ k; |9 H* z
                %text file output
9 h! u. u( C" i7 j( k                output_samples = zeros(1,floor(length(output)/8));  %extra zeros are not original data
$ ~  b, D5 I1 u: M; n9 P6 m                for i = 1:length(output_samples)2 M. X$ G* X6 u1 f, m/ a
                        output_samples(i) = bin2eight(output(1 + (i-1)*8: (i-1)*8 + 8));
0 h1 z+ f( B2 w! ]" d! p, m. g                end
; B. d4 ^4 k' F2 N, Z, y                file = fopen('OFDM_text_out.txt','wt+');4 y: Y7 h4 \- B$ O$ o4 ], n* @
                fwrite(file,output_samples,'char');
+ O/ N, ]" F& L5 a9 u0 Y/ u                fclose(file);) [5 [# h; \; n6 x
+ n. [5 ?5 H3 a) P$ y' {% N
                if do_QAM == 1
" k7 k  q, c$ P) R5 l9 ?% M                        %extra zeros are not original data( Z3 N4 R* I$ Z; f4 s0 O7 w
                        QAM_output_samples = zeros(1,floor(length(QAM_data_out)/8));  ' Y# P3 M+ u- Z( }
                        7 ~, q9 C/ Q3 z. _0 x
for i = 1:length(QAM_output_samples), R5 h; [' ?" q+ t. g+ \3 R
                                QAM_output_samples(i) = bin2eight(QAM_data_out(1 + (i-1)*8: (i-1)*8 + 8));  X6 a* b, Y1 b! Q) H5 t) Q5 t
                        end
; d7 k+ D/ d  B8 a: U* J                        file = fopen('QAM_text_out.txt','wt+');5 m8 B% X7 F, P$ A3 N5 y
                        fwrite(file,QAM_output_samples,'char');
, t9 A" X, }. o! O; M/ V: s8 B                        fclose(file);7 ?. O8 U$ K6 j+ a) G
                end
4 V+ |3 w3 L& E4 B, g        end
  u; Q1 X+ p0 r& h3 x) d( Q& v  D8 m5 f* k3 ?
        if file_input_type == 3
) x3 K! F) |% t) q& m                output_samples_big = zeros(1,floor(length(output)/8));  %extra zeros are not original data
# T& k6 L1 x5 c( b; N0 r* c                for i = 1:length(output_samples_big)8 @, n' M6 J; R' d
                        output_samples_big(i) = bin2eight(output(1 + (i-1)*8: (i-1)*8 + 8));0 }5 w  \  J. L. g$ Z3 }6 G$ k
                end
2 ?  L. s3 H/ t0 A                %convert dynamic range from 0:255 to -1:1& i" o3 v& y! |1 f$ X3 f
                output_samples = (output_samples_big-127)/128;
. w5 v, R* r3 L; I: |  E                %sound file output, m8 n, U2 Z8 d  C
                wavwrite(output_samples, 11025, 8, 'OFDM_out.wav')
9 i& a8 n# {* }/ e* p+ K* J! q                if do_QAM == 12 b1 G. b. X. K/ q" N# ?
                        QAM_data_out_big = zeros(1,floor(length(QAM_data_out)/8));! D  o! y: t9 P' O) p: Q' l; a
                        for i = 1:length(QAM_data_out_big)6 k- ]4 j; q0 f* B, L$ E$ L" U
                                QAM_data_out_big(i) = bin2eight(QAM_data_out(1 + (i-1)*8: (i-1)*8 + 8));6 G6 |8 U/ L2 ^
                        end3 z5 {5 p+ u" ~
                        %convert dynamic range from 0:255 to -1: 1. y* \" F' X! A- s3 H
                        QAM_output_samples = (QAM_data_out_big-127)/128;
5 |' z$ E' F- m: G' {" \$ g2 ?                        %sound file output
0 \% i% B7 D: q; L7 g' N                        wavwrite(QAM_output_samples, 11025, 8, 'QAM_out.wav')
: R/ U& ^7 c! Q; F3 w                end
2 a) Q: \4 R1 z. R1 A/ L* E        end
& O* |3 H9 `; ]& H; o; s        ; j) F: s0 [/ E4 }! `+ r
        if file_input_type == 4
/ ]& U# {' T5 ?( t  z                %image file output - not implemented                1 X' ~; N: S% \) z6 @5 J6 g
        end) H! ?7 Q4 |1 k) x& w
end
. s$ F0 v0 y9 \, s! s& P
" v- d$ Y, I$ o4、%Analysis.m2 Q: e' o3 d" Q
% Analysis
0 A' I  E% p5 S' ^disp(' '), disp('------------------------------------------------------------')
( x/ q8 D+ m8 ]8 c  ]* F5 jdisp('Preparing Analysis')/ g$ q$ F9 v8 a
figure(1), clf3 ^  L7 D1 D6 r+ i! e# V+ a. y
if (input_type == 1) & (test_input_type == 1)- n+ h5 s7 Z) R  H
        subplot(221), stem(data_in), title('OFDM Binary Input Data');: p# |# ~6 _" K5 C! |9 C8 w
        subplot(223), stem(output), title('OFDM Recovered Binary Data')
% F+ [& j* f( a; p2 eelse
+ p5 d& Z* D+ n" u9 V; N        subplot(221), plot(data_samples), title('OFDM Symbol Input Data');  B+ S% V) Y# M9 v, N/ a& o
        subplot(223), plot(output_samples), title('OFDM Recovered Symbols');  |& s- l0 y7 I6 }/ w8 d5 M9 }
end
& j$ O) G0 O) o2 P* Z$ ^0 T" E4 ysubplot(222), plot(xmit), title('Transmitted OFDM');
- r0 n0 y$ x* K. A( M- ^subplot(224), plot(recv), title('Received OFDM');4 O" L" [/ Y: H/ L! K1 H$ V
' L$ b( U( S" d! x, w/ [: r) ?3 N
$ t6 S' R2 R7 r5 b' W9 [
% dig_x_axis = (1:length(QAM_tx_data))/length(QAM_tx_data);4 h3 @9 Y0 ^1 {7 u
%         figure(4), clf, subplot(212)) ^3 A6 q, J3 \* n7 v& H# O  h
%         freq_data = abs(fft(QAM_rx_data));/ w1 D* W  ?. S/ {/ i
%         L = length(freq_data)/2;
+ ~. [* A; \7 d        8 m' F" q8 U# ~9 Z
dig_x_axis = (1: length(xmit))/length(xmit);
- s3 V" \0 N* Nfigure(2), clf+ }7 N0 R( J6 `3 u0 }0 l* R8 V4 U
7 U- c  c# j8 O. v
if channel_on ==1
/ }/ H+ q" K7 X7 @2 }        num = [1, zeros(1, d1-1), a1, zeros(1, d2-d1-1), a2];
& M, R0 g+ k; O( U% b" m        den = [1];
/ o5 h+ D" |$ Z, |0 E) w        [H, W] = freqz(num, den, 512);9 b: @* f, ~0 [
        mag = 20*log10(abs(H));
: f, _) L, {% j7 f$ S$ J8 t        phase = angle(H) * 180/pi;
7 i% p% {2 c  F/ E/ m        $ }9 N4 }7 O9 ?4 k9 }) d
        subplot(313)1 R. F$ T. A0 P; ?( ?9 b7 |
        freq_data = abs(fft(recv));
0 \, q' R8 S+ l) G# M( u- a& F' B        L = length(freq_data)/2;9 N5 j0 s+ X$ |+ }
        plot(dig_x_axis(1: L), freq_data(1: L))+ z( {, w* K5 x) V/ O9 R2 h6 s
        xlabel('FFT of Received OFDM')4 p  L  I( Z" N
        axis_temp = axis;
. i4 p9 M# t- |. ^4 h        
6 A! f( \; Q* V        subplot(311),3 x* [  j; n8 m* a% C
        freq_data = abs(fft(xmit));
/ X% {% p. Z# j2 z: X8 |0 v! i        plot(dig_x_axis(1: L), freq_data(1: L)), axis(axis_temp)
8 q# D$ y& C. j# m        title('FFT of Transmitted OFDM'): S4 B* t& e2 t- J: d' z  M" G
        
9 g% m) @8 n5 i. J( V) b5 s( J        subplot(312)
+ ?- V5 |$ O1 C0 [' b  N        plot(W/(2*pi),mag),
% g1 V+ b1 ?, x% z3 q7 {        ylabel('Channel Magnitude Response'), }1 u8 z  i  o' ~
else* L$ M, z9 G. t4 s: R
        subplot(212)        
( O3 y) [' [, O, i0 p( P& F        freq_data = abs(fft(recv));2 s3 S# @2 d, R; b% [! c3 G1 r( W
        L = length(freq_data)/2;& j9 ~+ g; o% a5 _' ^
        plot(dig_x_axis(1: L), freq_data(1: L))
- t& i8 i; H+ R) n+ R        xlabel('FFT of Received OFDM')* m1 A4 k; t6 K
        axis_temp = axis;( Y2 U7 S# K2 _* o% s
        % u8 ?  V+ @0 @% {/ L
        subplot(211),. v+ R( }% ]$ M) D% q/ a
        freq_data = abs(fft(xmit));( L$ N$ o' T# [' u+ }7 x
        plot(dig_x_axis(1: L), freq_data(1: L)), axis(axis_temp)5 e& |9 z  J' E+ |9 b+ a0 m# _# {! f
        title('FFT of Transmitted OFDM'): k, C" u. v2 H' D8 f
end6 `- F' h" O& y7 b3 G2 O4 t

% X$ ^5 U5 V4 ]% if file_input_type == 4* \6 |0 t; x2 a5 r- r8 B, c
%         figure(5)6 K" Y+ T& a: w0 Q
%         subplot(211)
: K0 T! Q  G& ^# u8 e: `%         image(data_in);
( C5 L6 T! j$ l' I/ t3 v%         colormap(map);' ^! s7 s8 p4 }/ p! B0 G, ~  i; k% K
%         subplot(212)
/ u& r; z, J) K%         image(output);& F0 a2 G9 C4 `7 s, {
%         colormap(map);8 a; M% I- Z6 r9 `# c! f+ g1 l) E
% end
8 c1 C2 M8 X3 d/ Z
% r! g& b+ y! Y  l% C* hif do_QAM == 1         % analyze if QAM was done2 d$ b5 Q* r# @
        
' k: s2 p2 d# x8 S) Z" {8 ?        figure(3), clf) W% @0 R1 Q! q
        if (input_type == 1) & (test_input_type == 1)
) X  N+ U) N) U) _                subplot(221), stem(data_in), title('QAM Binary Input Data');! v' @! A7 F' M9 e
                subplot(223), stem(QAM_data_out), title('QAM Recovered Binary Data')
9 _- I/ N$ w; I        else& q2 _, G  d: `
                subplot(221), plot(data_samples), title('QAM Symbol Input Data');2 O0 [' D6 ~& b3 d$ p9 G
                subplot(223), plot(QAM_output_samples), title('QAM Recovered Symbols');
2 a; ~- O/ V% }% K! h        end
, k$ L8 t1 C' G  }9 [        subplot(222), plot(QAM_tx_data), title('Transmitted QAM');+ A1 n9 w5 k# L
        subplot(224), plot(QAM_rx_data), title('Received QAM');
$ c$ c' x  x7 o+ i( O        
. @/ T2 G; d" I: i! V+ ^/ D( ?        dig_x_axis = (1: length(QAM_tx_data))/length(QAM_tx_data);6 O; w' b! ~' Z0 `0 [
        figure(4), clf
$ @5 g' y1 C9 O7 {7 I        
' f( N( ~2 y  r8 @        if channel_on ==1
* T# D; }5 K/ V5 e, P7 X' P                subplot(313)6 h2 q; \  S+ U; i' X2 g
                freq_data = abs(fft(QAM_rx_data));
) F% \8 y. H) u                L = length(freq_data)/2;
% X" \+ Q% B1 e1 j: D2 t                plot(dig_x_axis(1: L), freq_data(1: L)); g" U, s3 \& F. o+ \
                xlabel('FFT of Received QAM')
3 l- U. f) j( p  N8 t                axis_temp = axis;
1 }% x  l. @* {# s               
3 e, n& X9 [0 h9 e3 d                subplot(311),
7 z/ h% t9 [, \  }                freq_data = abs(fft(QAM_tx_data));6 w) u/ E2 ]' V2 t* k
                 plot(dig_x_axis(1: L),freq_data(1: L)), axis(axis_temp)1 ]7 O7 H6 G! b3 a6 Y+ B# W3 y
                title('FFT of Transmitted QAM')
2 p) |. h7 E4 O6 U/ F               
; f5 q! G2 B, b5 l# d                subplot(312)8 s; Z" f, I, T$ }# }
                plot(W/(2*pi),mag)$ O& I: M2 U  {" B' U
                ylabel('Channel Magnitude Response')
2 Q* W2 Y! N; D5 S' @        else& n4 z/ x, S: B1 m3 g! x; Z
                subplot(212)
2 v( J- h+ K. q8 t6 y                freq_data = abs(fft(QAM_rx_data));
8 G# n3 W- T6 K; C- r7 U                L = length(freq_data)/2;5 S# F  o! ]/ {% k
                plot(dig_x_axis(1: L), freq_data(1: L))8 r. ~! N! Y* V1 x1 e8 `( c
                title('FFT of Received QAM')
% D% `; W! {, F$ i: y                axis_temp = axis;4 `1 v$ y" N/ _$ M. b
                ; x/ G$ o5 C1 ~8 n6 _
                subplot(211),
% _2 ]; O+ H4 Q8 j6 |; j3 P                freq_data = abs(fft(QAM_tx_data));
4 J) g( T% T% b7 g- i                 plot(dig_x_axis(1: L),freq_data(1: L)), axis(axis_temp)$ d- e9 C$ ~% z5 t  Z( U! J
                title('FFT of Transmitted QAM')
2 Q* \) k; `3 O5 R        end
  f9 Q& @- X3 V) \                % D& V7 z) E5 X* e$ |9 ?/ t4 \2 ~/ H
        % Plots the QAM Received Signal Constellation, {' S" [4 d  M% K: Y2 s
        figure(5), clf, plot(xxx,yyy,'ro'), grid on, axis([-2.5 2.5 -2.5 2.5]), hold on
9 B  J5 S1 \$ Z3 @$ R& x/ q0 A* k: i% z8 S8 h4 A
%         % Overlay plot of transmitted constellation
4 Y3 y7 `  o% D8 n5 Y%         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];
( H# j- t' l% H* W0 H$ S%         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];6 M, S2 e7 Q, m, Z$ w& c
%         plot(x_const, y_const, 'b*')
+ v( z! t+ f# m+ i, i3 J8 Y* N. Z: H( J2 I% X2 x% p0 A8 ]
        % Overlay of constellation boundarys
6 r* }( P3 ?/ B/ B# U4 Z; y        x1 = [-2 -2]; x2 = [-1 -1]; x3 = [0 0]; x4 = [1 1]; x5 = [2 2]; x6 = [-2 2];
( {: f% K0 B* p        y1 = [-2 -2]; y2 = [-1 -1]; y3 = [0 0]; y4 = [1 1]; y5 = [2 2]; y6 = [-2 2];; T& u( }: G; ~- i' L& Y  w0 V
        plot(x1,y6), plot(x2,y6), plot(x3,y6), plot(x4,y6), plot(x5,y6)
% M) g+ L' R# s3 e$ Z' I8 k        plot(x6,y1), plot(x6,y2), plot(x6,y3), plot(x6,y4), plot(x6,y5)        5 c# q% O- m8 f- e

6 |2 l! M3 x- d& u2 ~" K  Y7 l        hold off
7 Y: w* w4 j# M4 _# T3 w7 Q7 `/ G        title('16-QAM Received Signal Constellation and Decision Boundarys')  Q1 B8 p% h( F7 _
        
: c1 G5 m& Q  s& `- Q        binary_err_bits_QAM = 0;7 X9 R1 B6 p. H# u
        for i = 1:length(data_in)
- K& ]7 R. u" Y; Q& t' X1 v                err = abs(data_in(i)-QAM_data_out(i));3 A1 K! Q# N) U
                if err > 03 l' [1 v7 L% O( H' b' F
                        binary_err_bits_QAM = binary_err_bits_QAM + 1;1 q: s: v7 m; m, U' ]! A7 @
                end
: I0 X* C" b- ]- d4 s        end5 [) J: E5 e/ P# n
        BER_QAM = 100 * binary_err_bits_QAM/data_length;
' s( m! J# O1 {. Yend' Y4 g; \. E! u9 M* d

6 ^# o2 S$ m* v/ P3 B& \% r6 Dfigure(6), clf' B$ l; P& O4 O
if channel_on == 1
9 h# o- o5 g/ k; ]$ H        subplot(211), plot(W/(2*pi),mag),title('Channel Magnitude Response')
, ]( ?; |8 R! a9 U        xlabel('Digital Frequency'),ylabel('Magnitude in dB')) z+ f1 w- J* k) P  z9 W/ r' t( n
        subplot(212), plot(W/(2*pi),phase),title('Channel Phase Response')/ o6 @! b; W; S
        xlabel('Digital Frequency'),ylabel('Phase in Degrees')! H, K8 b! Q0 W% {1 V
else
5 k7 h7 E' X+ B! @/ w        title('Channel is turned off - No frequency response to plot')
( R$ H+ t, o3 Y5 U4 nend
0 N, A! j# n* W; a* }
. ?* h; r- ]) g4 }6 f! d) W: o% Compare output to input and count errors
9 j" K) a9 c% ~' zbinary_err_bits_OFDM = 0;+ m8 h6 \6 `( D; [! [
for i = 1:length(data_in)
; s! Z: P5 J, @* {  `1 x' c        err = abs(data_in(i)-output(i));
$ h9 G* Z1 n4 Q; Z7 a        if err > 0+ ]* e( y5 a% |! T% q# y; D
                binary_err_bits_OFDM = binary_err_bits_OFDM +1;( [0 A8 M  L) |) L( B) r  [# f
        end
+ b: B( g8 R: }* ?, d  ?4 \4 @end1 z- J* Y& M* s/ {4 ~3 y
BER_OFDM = 100 * binary_err_bits_OFDM/data_length;
; ~* F+ T2 C' v: Bdisp(strcat('OFDM: BER=', num2str(BER_OFDM,3), ' %'))
5 o) H2 I8 s" j  l6 ddisp(strcat('      Number of error bits=', num2str(binary_err_bits_OFDM)))/ c* G. d2 B' b% Y5 o: l2 q
. ^) j( H% [  k
if (do_QAM == 1)
: T7 e" S; Z( ]        disp(strcat('QAM:  BER=', num2str(BER_QAM,3), ' %'))4 e: y9 X) z7 k9 V1 a
        disp(strcat('      Number of error bits=', num2str(binary_err_bits_QAM)))
4 W1 o3 q- R* t% vend
  ~8 D) l/ _6 a# s* I" n9 z! D
4 h  s8 @6 Y+ K2 @- v% Display text file before and after modulation6 ~/ E( Z5 M  U  N. {" w# ]
if (input_type == 2) & (file_input_type == 2)
- b/ Q# e) L0 N, L. J! }3 H        original_text_file = char(data_samples')* Z$ a) J9 d7 b6 [7 Z) R
        if do_QAM ==1
' X; M! e+ D, w' \9 @  k, a                edit QAM_text_out.txt0 _$ R9 }4 {9 H' l8 W) n& ?+ z
        end
+ S6 f0 C' W, f* L$ Y9 R4 ~6 E        edit OFDM_text_out.txt
% X8 @, `5 m& B1 d* C/ t  jend/ I% l9 F& b6 J% D& ]! ~
' i* M: D" \1 W% f4 K+ A
% Listen to sounds* `0 _6 @* r0 Z# |" c
if (input_type == 2) & (file_input_type == 3)8 r1 Q1 S8 N6 P: _4 B$ G5 N! @
        do_again = '1';) f. x" Y2 R6 H5 E/ _( V
        while ( ~(isempty(do_again)) )
1 D& Y) E6 t9 p5 m# q% b0 E2 y                disp(' ')% A6 H0 z# @$ z. c1 p  Q
                disp('Press any key to hear the original sound'), pause6 T! E1 i' a, a
                sound(data_samples,11025)
( w9 A8 p0 G) I0 L. M/ ]/ a                disp('Press any key to hear the sound after OFDM transmission'), pause
8 X. W) q5 W; A1 N: \- W3 c                sound(output_samples,11025)
$ ?% }) `- b, B0 Z5 X% M0 M, o                if do_QAM == 1
( e( N# P3 b2 K0 v; t( X                        disp('Press any key to hear the sound after QAM transmission'), pause: F' s' K0 t1 @0 w5 M
                        sound(QAM_output_samples,11025)& a0 h% o9 }7 T6 S( D3 F) u, z% ]
                end
2 N  V' m3 s- j% J                do_again = '';
. S# r. K$ K( O; F4 u5 N                do_again = input('Enter "1" to hear the sounds again or press "Return" to end  ', 's');
4 b' O& c% M9 |; g' @        end
8 M$ }5 t! v  Hend' R' S/ ^1 U5 V, {+ q: i  \/ |( ]
" K3 a  V4 @; p6 b
- @( B* n4 I# u4 n5 c9 t
游客,如果您要查看本帖隐藏内容请回复

该用户从未签到

3#
发表于 2020-4-21 13:55 | 只看该作者
看看楼主分享的OFDM的仿真程序。

该用户从未签到

2#
发表于 2020-4-20 16:03 | 只看该作者
有详细的WORD说明最好了,怕我看不懂。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-24 10:55 , Processed in 0.234375 second(s), 27 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表