www.pudn.com > ofdm_simulation_primary.rar > ofdm_simulation_primary.m


clear all; 
close all; 
fprintf('OFDM仿真\n\n');  
        %fprintf:Write formatted(格式化的) data to file 
IFFT_bin_length=1024; 
carrier_count=200; 
bits_per_symbol=2; 
symbols_per_carrier=50; 
SNR=input('SNR=');%输入SNR 
baseband_out_length=carrier_count*symbols_per_carrier*bits_per_symbol;%baseband_out_length=20000 
carriers=(1:carrier_count)+(floor(IFFT_bin_length/4)-floor(carrier_count/2));%1×200的数组,取值157~356 
        %floor:Round towards minus infinity, 
        %            rounds the elements of A to the nearest integers less than or equal to A(四舍五入) 
conjugate_carriers=IFFT_bin_length-carriers+2;%1×200的数组,取值869~670 
%transmiting signal 信号发射 
baseband_out=round(rand(1,baseband_out_length));%1×20000的数组,取值为0或1 
        %round:Round to nearest integer;rand:Uniformly distributed random numbers and arrays 
convert_matrix=reshape(baseband_out,bits_per_symbol,length(baseband_out)/bits_per_symbol);%2×10000的数组,取值为0或1 
        %reshape:Reshape array 
for k=1:(length(baseband_out)/bits_per_symbol) 
    %length:Length of vector     
    modulo_baseband(k)=0; 
    for i=1:bits_per_symbol 
        modulo_baseband(k)=modulo_baseband(k)+convert_matrix(i,k)*2^(bits_per_symbol-i);%1×10000的数组,取值为整数0~3 
    end 
end 
carrier_matrix=reshape(modulo_baseband,carrier_count,symbols_per_carrier)';%50×200的数组,取值为整数0~3 
% QDPSK modulation QDPSK调制 
carrier_matrix=[zeros(1,carrier_count);carrier_matrix];%51×200的数组,取值为整数0~3 
        %zeros:Create an array of all zeros 
for i=2:(symbols_per_carrier+1) 
    carrier_matrix(i,:)=rem(carrier_matrix(i,:)+carrier_matrix(i-1,:),2^bits_per_symbol); 
        %rem:Remainder(余数) after division 
end 
carrier_matrix=carrier_matrix*((2*pi)/(2^bits_per_symbol)); 
[X,Y]=pol2cart(carrier_matrix,ones(size(carrier_matrix,1),size(carrier_matrix,2)));%X,Y都是51×200的数组 
        %pol2cart: Transform polar or cylindrical coordinates to Cartesian(将极坐标或柱坐标转化成笛卡儿坐标) 
        %ones: Create an array of all ones 
        %size:Array dimensions 
complex_carrier_matrix=complex(X,Y);%51×200的复数数组 
        %complex:Construct complex data(复数) from real and imaginary components 
%loading training symbols 加训练序列 
training_symbols=[1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 ... 
    1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 ... 
    j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j ... 
    1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1 1 j j 1 -1 -j -j -1];%1×200的数组 
training_symbols=cat(1,training_symbols,training_symbols);%2×200的数组 
        %cat:Concatenate(连接) arrays along specified dimension(维数) 
training_symbols=cat(1,training_symbols,training_symbols);%4×200的数组 
complex_carrier_matrix=cat(1,training_symbols,complex_carrier_matrix);%55×200的复数数组 
IFFT_modulation=zeros(4+symbols_per_carrier+1,IFFT_bin_length);%55×1024的数组,取值全0 
IFFT_modulation(:,carriers)=complex_carrier_matrix;%55×1024的数组,加载complex_carrier_matrix 
IFFT_modulation(:,conjugate_carriers)=conj(complex_carrier_matrix);%55×1024的数组,加载complex_carrier_matrix的共轭 
        %conj:Complex conjugate(共轭) 
time_wave_matrix=ifft(IFFT_modulation');%1024×55的矩阵,ifft的结果 
        %ifft:Inverse discrete(离散) Fourier transform 
time_wave_matrix=time_wave_matrix'; 
for i=1:4+symbols_per_carrier+1 
    windowed_time_wave_matrix(i,:)=real(time_wave_matrix(i,:));%55×1024的数组 
        %real:Real part of complex number 
end 
ofdm_modulation=reshape(windowed_time_wave_matrix',1,IFFT_bin_length*(4+symbols_per_carrier+1));%1×56320的数组 
Tx_data=ofdm_modulation;%1×56320的数组 
%channel 信道 
            %多径传输 
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));%1×56320的数组 
for i=1+d1:length(Tx_data) 
    copy1(i)=a1*Tx_data(i-d1); 
end 
copy2=zeros(size(Tx_data));%1×56320的数组 
for i=1+d2:length(Tx_data) 
    copy1(i)=a2*Tx_data(i-d2); 
end 
copy3=zeros(size(Tx_data));%1×56320的数组 
for i=1+d3:length(Tx_data) 
    copy1(i)=a3*Tx_data(i-d3); 
end 
copy4=zeros(size(Tx_data));%1×56320的数组 
for i=1+d4:length(Tx_data) 
    copy1(i)=a4*Tx_data(i-d4); 
end 
Tx_data=Tx_data+copy1+copy2;%1×56320的数组 
Tx_signal_power=var(Tx_data);%Tx_signal_power的值 
        %var:Variance(方差) 
linear_SNR=10^(SNR /10);%linear_SNR的值 
noise_sigma=Tx_signal_power/linear_SNR;%noise_sigma的值 
noise_scale_factor=sqrt(noise_sigma);%noise_scale_factor的值 
        %sqrt:Square root 
noise=randn(1,length(Tx_data))*noise_scale_factor;%1×56320的数组 
        %randn:Normally distributed random numbers and arrays 
Rx_Data=Tx_data+noise;%1×56320的数组 
%signal reception 信号接收 
Rx_Data_matrix=reshape(Rx_Data,IFFT_bin_length,4+symbols_per_carrier+1);%1024×55的数组 
Rx_spectrum=fft(Rx_Data_matrix);%1024×55的数组,fft的结果 
        %fft:Discrete Fourier transform 
Rx_carriers=Rx_spectrum(carriers,:)';%55×200的数组 
Rx_training_symbols=Rx_carriers((1:4),:);%4×200的数组 
Rx_carriers=Rx_carriers((5:55),:);%51×200的数组 
%signal estimate 信道估计(?有问题?) 
Rx_training_symbols=Rx_training_symbols./training_symbols;%4×200的数组 
Rx_training_symbols_deno=Rx_training_symbols.^2;%4×200的数组 
Rx_training_symbols_deno=Rx_training_symbols_deno(1,:)+Rx_training_symbols_deno(2,:)+... 
    Rx_training_symbols_deno(3,:)+Rx_training_symbols_deno(4,:);%1×200的数组 
Rx_training_symbols_nume=Rx_training_symbols(1,:)+Rx_training_symbols(2,:)+... 
    Rx_training_symbols(3,:)+Rx_training_symbols(4,:);%1×200的数组 
Rx_training_symbols_nume=conj(Rx_training_symbols_nume);%1×200的数组,Rx_training_symbols_nume的共轭 
Rx_training_symbols=Rx_training_symbols_nume./Rx_training_symbols_deno;%1×200的数组 
Rx_training_symbols_2=cat(1,Rx_training_symbols,Rx_training_symbols);%2×200的数组 
Rx_training_symbols_4=cat(1,Rx_training_symbols_2,Rx_training_symbols_2);%4×200的数组 
Rx_training_symbols_8=cat(1,Rx_training_symbols_4,Rx_training_symbols_4);%8×200的数组 
Rx_training_symbols_16=cat(1,Rx_training_symbols_8,Rx_training_symbols_8);%16×200的数组 
Rx_training_symbols_32=cat(1,Rx_training_symbols_16,Rx_training_symbols_16);%32×200的数组 
Rx_training_symbols_48=cat(1,Rx_training_symbols_32,Rx_training_symbols_16);%48×200的数组 
Rx_training_symbols_50=cat(1,Rx_training_symbols_48,Rx_training_symbols_2);%50×200的数组 
Rx_training_symbols=cat(1,Rx_training_symbols_50,Rx_training_symbols);%51×200的数组 
Rx_carriers=Rx_training_symbols.*Rx_carriers;%51×200的数组 
Rx_phase=angle(Rx_carriers)*(180/pi);%51×200的数组 
        %angle:Phase angle(相角) 
phase_negative=find(Rx_phase<0);%phase_negative的数组 
        %find:Find indices(下标) and values of nonzero elements 
Rx_phase(phase_negative)=rem(Rx_phase(phase_negative)+360,360);%51×200的数组 
Rx_decoded_phase=diff(Rx_phase);%50×200的数组 
        %diff:Differences(差分) and approximate derivatives(导数) 
phase_negative=find(Rx_decoded_phase<0);%phase_negative的数组 
Rx_decoded_phase(phase_negative)=rem(Rx_decoded_phase(phase_negative)+360,360);%50×200的数组 
% QDPSK demodulation QDPSK解调 
base_phase=360/2^bits_per_symbol;%base_phase的值 
delta_phase=base_phase/2;%delta_phase的值 
Rx_decoded_symbols=zeros(size(Rx_decoded_phase,1),size(Rx_decoded_phase,2));%50×200的数组 
for i=1:(2^bits_per_symbol-1) 
    center_phase=base_phase*i;%center_phase的值 
    plus_delta=center_phase+delta_phase;%plus_delta的值 
    minus_delta=center_phase-delta_phase;%minus_delta的值 
    decoded=find((Rx_decoded_phase<=plus_delta)&(Rx_decoded_phase>minus_delta));%decoded的数组 
    Rx_decoded_symbols(decoded)=i;%50×200的数组 
end 
Rx_serial_symbols=reshape(Rx_decoded_symbols',1,size(Rx_decoded_symbols,1)*... 
    size(Rx_decoded_symbols,2));%1×10000的数组 
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);%1×10000的数组 
    else 
        Rx_binary_matrix(i,:)=Rx_serial_symbols;%2×10000的矩阵 
    end 
end 
baseband_in=reshape(Rx_binary_matrix,1,size(Rx_binary_matrix,1)*size(Rx_binary_matrix,2));%1×20000的数组 
%BER calculation 误码率计算 
bit_errors=find(baseband_in~=baseband_out);%bit_errors的数组 
bit_error_count=size(bit_errors,2);%bit_error_count的值 
total_bits=size(baseband_out,2);%total_bits的值 
bit_error_rate=bit_error_count/total_bits;%bit_error_rate的值 
fprintf('BER=%f\n',bit_error_rate);