⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ofdm_code.m

📁 OFDM 编写源代码 matlab ofdm coder
💻 M
📖 第 1 页 / 共 2 页
字号:
%   Test by lavabin
%   Another way of matrix transition
ofdm_modulation_tmp = windowed_time_wave_matrix.';
ofdm_modulation_test = ofdm_modulation_tmp(:)';
if (ofdm_modulation_test == ofdm_modulation)
  fprintf('ofdm_modulation_test == ofdm_modulation \n\n\n');
else
  fprintf('ofdm_modulation_test ~= ofdm_modulation \n\n\n');
end 
% We get the result "ofdm_modulation_test == ofdm_modulation" .
%-------------------------------------------------------------------------

Tx_data = ofdm_modulation;

% --------------------------------------------- %
%                   信道模拟                     %
% --------------------------------------------- %
d1 = 4; a1 = 0.2; d2 = 5; a2 = 0.3; d3 = 6; a3 = 0.4;
d4 = 7; a4 = 0.5;

copy1 = zeros(size(Tx_data)) ;

for i = 1 + d1: length(Tx_data)
copy1(i) = a1*Tx_data( i - d1) ;
end

copy2 = zeros(size(Tx_data) ) ;

for i = 1 + d2: length( Tx_data)
copy2(i) = a2*Tx_data( i - d2) ;
end

copy3 = zeros(size(Tx_data) ) ;

for i = 1 + d3: length(Tx_data)
copy3(i) = a3*Tx_data ( i - d3) ;
end

copy4 = zeros(size(Tx_data) ) ;

for i = 1 + d4: length( Tx_data)
copy4(i) = a4*Tx_data(i - d4) ;
end

Tx_data = Tx_data + copy1 + copy2 + copy3 + copy4;
% 4 multi-paths

Tx_signal_power = var(Tx_data);
%-------------------------------------------------------------------------
% 函数说明:
%  VAR Variance.
%     For vectors, Y = VAR(X) returns the variance of the values in X.  For
%     matrices, Y is a row vector containing the variance of each column of
%     X.

linear_SNR = 10^( SNR /10) ;
noise_sigma = Tx_signal_power / linear_SNR;
noise_scale_factor = sqrt(noise_sigma) ;%平方根
noise = randn(1, length(Tx_data) )*noise_scale_factor;
%-------------------------------------------------------------------------
% 函数说明:
% Y = randn(m,n) or Y = randn([m n]) returns an m-by-n matrix of random
% entries.
% The randn function generates arrays of random numbers whose elements are
% normally distributed with mean 0 and variance 1.

Rx_Data = Tx_data + noise;

% --------------------------------------------- %
%                  信号接收                      %
% --------------------------------------------- %
Rx_Data_matrix = reshape(Rx_Data, IFFT_bin_length, 4 + symbols_per_carrier + 1) ;
Rx_spectrum = fft(Rx_Data_matrix) ; 

% %  Suppose precise synchronazition between Tx and Rx
Rx_carriers = Rx_spectrum( carriers, : )';
Rx_training_symbols = Rx_carriers( (1: 4) , : ) ;
Rx_carriers = Rx_carriers((5: 55), : ) ;

% --------------------------------------------- %
%                    信道估计                    %
% --------------------------------------------- %
Rx_training_symbols = Rx_training_symbols./ training_symbols;
Rx_training_symbols_deno = Rx_training_symbols.^2;
Rx_training_symbols_deno = Rx_training_symbols_deno(1,:)+Rx_training_symbols_deno(2,:)+Rx_training_symbols_deno(3,:)+Rx_training_symbols_deno(4,:) ;
Rx_training_symbols_nume = Rx_training_symbols(1, : ) +Rx_training_symbols(2, : ) + Rx_training_symbols(3, : ) +Rx_training_symbols(4, : ) ;
Rx_training_symbols_nume = conj(Rx_training_symbols_nume) ;
% % 取4个向量的导频符号是为了进行平均优化
% % 都是针对 “行向量”即单个的OFDM符号 进行操作
% % 原理:寻求1/H,对FFT之后的数据进行频域补偿
% % 1/H = conj(H)/H^2 because H^2 = H * conj(H) 

Rx_training_symbols = Rx_training_symbols_nume./Rx_training_symbols_deno;
Rx_training_symbols_2 = cat(1, Rx_training_symbols,Rx_training_symbols) ;
Rx_training_symbols_4 = cat(1, Rx_training_symbols_2,Rx_training_symbols_2) ;
Rx_training_symbols_8 = cat(1, Rx_training_symbols_4,Rx_training_symbols_4) ;
Rx_training_symbols_16 = cat(1, Rx_training_symbols_8, Rx_training_symbols_8) ;
Rx_training_symbols_32 = cat(1, Rx_training_symbols_16, Rx_training_symbols_16) ;
Rx_training_symbols_48 = cat(1, Rx_training_symbols_32, Rx_training_symbols_16) ;
Rx_training_symbols_50 = cat(1, Rx_training_symbols_48, Rx_training_symbols_2) ;
Rx_training_symbols = cat(1, Rx_training_symbols_50,Rx_training_symbols) ;

Rx_carriers = Rx_training_symbols.*Rx_carriers;
% 进行频域单抽头均衡

Rx_phase = angle(Rx_carriers)*(180/pi) ;
phase_negative = find(Rx_phase < 0) ;
%-------------------------------------------------------------------------
% 函数说明:
%     FIND   Find indices of nonzero elements.
%     I = FIND(X) returns the linear indices of the array X that are nonzero.
%     X may be a logical expression. Use IND2SUB(I,SIZE(X)) to calculate
%     multiple subscripts from the linear indices I.
%----------------------Test of Using "rem"---------------------------------
Rx_phase1 = Rx_phase; 
Rx_phase2 = Rx_phase;
Rx_phase1(phase_negative) = rem(Rx_phase1(phase_negative) + 360, 360) ;
Rx_phase2(phase_negative) = Rx_phase2(phase_negative) + 360 ;
if Rx_phase2(phase_negative) == Rx_phase1(phase_negative)
    fprintf('\n There is no need using rem in negative phase transition.\n')
else
    fprintf('\n We need to use rem in negative phase transition.\n')    
end
%-------------------------------------------------------------------------
Rx_phase(phase_negative) = rem(Rx_phase(phase_negative) + 360, 360) ;
% % 把负的相位转化为正的相位

Rx_decoded_phase = diff(Rx_phase) ;
% %  这也是为什么要在前面加上初始相位的原因 
% % “Here a row vector of zeros is between training symbols and data symbols!!!”
%-------------------------------------------------------------------------
% 函数说明:
% %     DIFF Difference and approximate derivative.
% %     DIFF(X), for a vector X, is [X(2)-X(1)  X(3)-X(2) ... X(n)-X(n-1)].
% %     DIFF(X), for a matrix X, is the matrix of row differences,
% %        [X(2:n,:) - X(1:n-1,:)].
%------------------------Test Codes --------------------------------------
% %  a = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
% %  b = a;
% %      for i = 2:4
% %        b(i,:) = b(i-1,:) + b(i,:);
% %      end
% %  c = diff(b);
%-----------------------Test Result --------------------------------------
% %  a =                  Modulating signal
% %      1     2     3
% %      4     5     6
% %      7     8     9
% %     10    11    12
% %  b =                  Modulated signal
% %      1     2     3
% %      5     7     9
% %     12    15    18
% %     22    26    30
% %  c =                  Recovered signal
% %      4     5     6
% %      7     8     9
% %     10    11    12
% %
% ----------------------------------------------------------------------------
% %   Name                   Size                    Bytes  Class
% %   Rx_phase              51x200                   81600  double array
% %   Rx_decoded_phase      50x200                   80000  double array
% % ---------------------------------------------------------------------------- 
phase_negative = find(Rx_decoded_phase < 0) ;
Rx_decoded_phase(phase_negative)= rem(Rx_decoded_phase(phase_negative) + 360, 360) ;
% % 再次把负的相位转化为正的相位

% --------------------------------------------- %
%                   QDPSK解调                   %
% --------------------------------------------- %
base_phase = 360 /2^bits_per_symbol;
delta_phase = base_phase /2;
Rx_decoded_symbols = zeros(size(Rx_decoded_phase,1),size(Rx_decoded_phase,2)) ;
%
for i = 1: (2^bits_per_symbol - 1)
    center_phase = base_phase*i;
    plus_delta = center_phase + delta_phase;  % Decision threshold 1
    minus_delta = center_phase - delta_phase; % Decision threshold 2
    decoded = find((Rx_decoded_phase <= plus_delta)&(Rx_decoded_phase > minus_delta)) ;
    Rx_decoded_symbols(decoded) = i;
end
% %  仅仅对三个区域进行判决
% %  剩下的区域就是零相位的空间了
% %  这个区域在定义解调矩阵时已经定义为零

Rx_serial_symbols = reshape(Rx_decoded_symbols',1,size(Rx_decoded_symbols, 1)*size(Rx_decoded_symbols,2)) ;
for i = bits_per_symbol: -1: 1
    if i ~= 1
        Rx_binary_matrix(i, : ) = rem(Rx_serial_symbols, 2) ;
        Rx_serial_symbols = floor(Rx_serial_symbols/2) ;
    else
    Rx_binary_matrix( i, : ) = Rx_serial_symbols;
    end
end
% % Integer to binary

baseband_in = reshape(Rx_binary_matrix, 1,size(Rx_binary_matrix, 1)*size(Rx_binary_matrix, 2) ) ;

% --------------------------------------------- %
%                   误码率计算                   %
% --------------------------------------------- %
bit_errors = find(baseband_in ~= baseband_out) ;
% % find的结果 其每个元素为满足逻辑条件的输入向量的标号,其向量长度也就是收发不一样的bit的个数

bit_error_count = size(bit_errors, 2) ;
total_bits = size( baseband_out, 2) ;
bit_error_rate = bit_error_count/ total_bits;
fprintf ( '%f \n',bit_error_rate) ;

% --------------------------------------------- %
%                   The END                     %
% --------------------------------------------- %
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -