MATLAB 프로그램: OFDM 신호 전송 및 수신

clear all;
close all;
carrier_count=200;%    
symbols_per_carrier=12;%        
bits_per_symbol=4;%       ,16QAM  
IFFT_bin_length=512;%FFT  
PrefixRatio=1/4;%     OFDM      1/6~1/4
GI=PrefixRatio*IFFT_bin_length ;%   OFDM            1/4*IFFT_bin_length          128
beta=1/32;%       
GIP=beta*(IFFT_bin_length+GI);%       20
SNR=15; %   dB
%==================================================
%================    ===================================
baseband_out_length = carrier_count * symbols_per_carrier * bits_per_symbol;%          200*12*4
carriers = (1:carrier_count) + (floor(IFFT_bin_length/4) - floor(carrier_count/2));%                  IFFT   
conjugate_carriers = IFFT_bin_length - carriers + 2;%                  IFFT   
rand( 'twister',0);
baseband_out=round(rand(1,baseband_out_length));%            
%==============16QAM  ====================================

complex_carrier_matrix=qam16(baseband_out);%   

complex_carrier_matrix=reshape(complex_carrier_matrix',carrier_count,symbols_per_carrier)';%symbols_per_carrier*carrier_count   

figure(1);
plot(complex_carrier_matrix,'*r');%16QAM      
axis([-4, 4, -4, 4]);
grid on
%=================IFFT===========================
IFFT_modulation=zeros(symbols_per_carrier,IFFT_bin_length);% 0  IFFT_bin_length IFFT    
IFFT_modulation(:,carriers ) = complex_carrier_matrix ;%        ,        
IFFT_modulation(:,conjugate_carriers ) = conj(complex_carrier_matrix);%      
%========================================================
figure(2);
stem(0:IFFT_bin_length-1, abs(IFFT_modulation(2,1:IFFT_bin_length)),'b*-')%   OFDM     
grid on
axis ([0 IFFT_bin_length -0.5 4.5]);
ylabel('Magnitude');
xlabel('IFFT Bin');
title('OFDM Carrier Frequency Magnitude');

figure(3);
plot(0:IFFT_bin_length-1, (180/pi)*angle(IFFT_modulation(2,1:IFFT_bin_length)), 'go')
hold on
stem(0:carriers-1, (180/pi)*angle(IFFT_modulation(2,1:carriers)),'b*-');%   OFDM     
stem(0:conjugate_carriers-1, (180/pi)*angle(IFFT_modulation(2,1:conjugate_carriers)),'b*-');
axis ([0 IFFT_bin_length -200 +200])
grid on
ylabel('Phase (degrees)')
xlabel('IFFT Bin')
title('OFDM Carrier Phase')
%=================================================================

signal_after_IFFT=ifft(IFFT_modulation,IFFT_bin_length,2);%OFDM    IFFT  
time_wave_matrix =signal_after_IFFT;%      ,          , ITTF  ,N         ,       OFDM  
figure(4);
subplot(3,1,1);
plot(0:IFFT_bin_length-1,time_wave_matrix(2,:));%        
axis([0, 700, -0.2, 0.2]);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('OFDM Time Signal, One Symbol Period');

%===========================================================
%=====================         ====================================
XX=zeros(symbols_per_carrier,IFFT_bin_length+GI+GIP);%GI=128,GIP=20
for k=1:symbols_per_carrier;%12
    for i=1:IFFT_bin_length;%512
        XX(k,i+GI)=signal_after_IFFT(k,i);%129--640
    end
    for i=1:GI;%1--128
        XX(k,i)=signal_after_IFFT(k,i+IFFT_bin_length-GI);%        % 128       
    end
    for j=1:GIP;
        XX(k,IFFT_bin_length+GI+j)=signal_after_IFFT(k,j);%         20       
    end
end

time_wave_matrix_cp=XX;%                 ,    OFDM     IFFT_bin_length+GI+GIP=660
subplot(3,1,2);
plot(0:length(time_wave_matrix_cp)-1,time_wave_matrix_cp(2,:));%               
axis([0, 700, -0.2, 0.2]);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('OFDM Time Signal with CP, One Symbol Period');



%==============OFDM    ==========================================
windowed_time_wave_matrix_cp=zeros(1,IFFT_bin_length+GI+GIP);
for i = 1:symbols_per_carrier %12
windowed_time_wave_matrix_cp(i,:) = real(time_wave_matrix_cp(i,:)).*rcoswindow(beta,IFFT_bin_length+GI)';%        
end  
subplot(3,1,3);
plot(0:IFFT_bin_length-1+GI+GIP,windowed_time_wave_matrix_cp(2,:));%        
axis([0, 700, -0.2, 0.2]);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('OFDM Time Signal Apply a Window , One Symbol Period');


%========================      ,    ==================================================
windowed_Tx_data=zeros(1,symbols_per_carrier*(IFFT_bin_length+GI)+GIP);
windowed_Tx_data(1:IFFT_bin_length+GI+GIP)=windowed_time_wave_matrix_cp(1,:);
for i = 1:symbols_per_carrier-1 ;
    windowed_Tx_data((IFFT_bin_length+GI)*i+1:(IFFT_bin_length+GI)*(i+1)+GIP)=windowed_time_wave_matrix_cp(i+1,:);%    ,            
end

%=======================================================
Tx_data_withoutwindow =reshape(time_wave_matrix_cp',(symbols_per_carrier)*(IFFT_bin_length+GI+GIP),1)';%    ,               
Tx_data =reshape(windowed_time_wave_matrix_cp',(symbols_per_carrier)*(IFFT_bin_length+GI+GIP),1)';%                    
%=================================================================
temp_time1 = (symbols_per_carrier)*(IFFT_bin_length+GI+GIP);%                    
figure (5)
subplot(2,1,1);
plot(0:temp_time1-1,Tx_data );%                  
grid on
ylabel('Amplitude (volts)')
xlabel('Time (samples)')
title('OFDM Time Signal')
temp_time2 =symbols_per_carrier*(IFFT_bin_length+GI)+GIP;
subplot(2,1,2);
plot(0:temp_time2-1,windowed_Tx_data);%                   
grid on
ylabel('Amplitude (volts)')
xlabel('Time (samples)')
title('OFDM Time Signal')

%=================         ==================================
symbols_per_average = ceil(symbols_per_carrier/5);%    1/5,10 
avg_temp_time = (IFFT_bin_length+GI+GIP)*symbols_per_average;%  ,10   ,10   
averages = floor(temp_time1/avg_temp_time);
average_fft(1:avg_temp_time) = 0;%  5 
for a = 0:(averages-1)
 subset_ofdm = Tx_data_withoutwindow (((a*avg_temp_time)+1):((a+1)*avg_temp_time));%
 subset_ofdm_f = abs(fft(subset_ofdm));%          
 average_fft = average_fft + (subset_ofdm_f/averages);%       5 ,    FFT,    
end
average_fft_log = 20*log10(average_fft);
figure (6)
subplot(2,1,1);
plot((0:(avg_temp_time-1))/avg_temp_time, average_fft_log)%     0/avg_temp_time  :  (avg_temp_time-1)/avg_temp_time
hold on
plot(0:1/IFFT_bin_length:1, -35, 'rd')
grid on
axis([0 0.5 -40 max(average_fft_log)])
ylabel('Magnitude (dB)')
xlabel('Normalized Frequency (0.5 = fs/2)')
title('OFDM Signal Spectrum without windowing')
%===============         =================================
symbols_per_average = ceil(symbols_per_carrier/5);%    1/5,10 
avg_temp_time = (IFFT_bin_length+GI+GIP)*symbols_per_average;%  ,10   ,10   
averages = floor(temp_time1/avg_temp_time);
average_fft(1:avg_temp_time) = 0;%  5 
for a = 0:(averages-1)
 subset_ofdm = Tx_data(((a*avg_temp_time)+1):((a+1)*avg_temp_time));%                      
 subset_ofdm_f = abs(fft(subset_ofdm));%     
 average_fft = average_fft + (subset_ofdm_f/averages);%       5 ,    FFT,    
end
average_fft_log = 20*log10(average_fft);
subplot(2,1,2)
plot((0:(avg_temp_time-1))/avg_temp_time, average_fft_log)%     0/avg_temp_time  :  (avg_temp_time-1)/avg_temp_time
hold on
plot(0:1/IFFT_bin_length:1, -35, 'rd')
grid on
axis([0 0.5 -40 max(average_fft_log)])
ylabel('Magnitude (dB)')
xlabel('Normalized Frequency (0.5 = fs/2)')
title('Windowed OFDM Signal Spectrum')
%====================    ============================================
Tx_signal_power = var(windowed_Tx_data);%      
linear_SNR=10^(SNR/10);%      
noise_sigma=Tx_signal_power/linear_SNR;
noise_scale_factor = sqrt(noise_sigma);%   sigma
noise=randn(1,((symbols_per_carrier)*(IFFT_bin_length+GI))+GIP)*noise_scale_factor;%          

%noise=wgn(1,length(windowed_Tx_data),noise_sigma,'complex');%   GAUSS      

Rx_data=windowed_Tx_data +noise;%         
%=====================       /           ==========================================
Rx_data_matrix=zeros(symbols_per_carrier,IFFT_bin_length+GI+GIP);
for i=1:symbols_per_carrier;
    Rx_data_matrix(i,:)=Rx_data(1,(i-1)*(IFFT_bin_length+GI)+1:i*(IFFT_bin_length+GI)+GIP);%    
end
Rx_data_complex_matrix=Rx_data_matrix(:,GI+1:IFFT_bin_length+GI);%           ,        

%============================================================
%================================================================

%==============================================================
%                      OFDM     16QAM  
%=================FFT  =================================
Y1=fft(Rx_data_complex_matrix,IFFT_bin_length,2);%OFDM    FFT  
Rx_carriers=Y1(:,carriers);%  IFFT/FFT     0,        
Rx_phase =angle(Rx_carriers);%       
Rx_mag = abs(Rx_carriers);%       
figure(7);
polar(Rx_phase, Rx_mag,'bd');%                
%======================================================================


[M, N]=pol2cart(Rx_phase, Rx_mag); %           

Rx_complex_carrier_matrix = complex(M, N);%    
figure(8);
plot(Rx_complex_carrier_matrix,'*r');%XY          
axis([-4, 4, -4, 4]);
grid on
%====================16qam  ==================================================
Rx_serial_complex_symbols = reshape(Rx_complex_carrier_matrix',size(Rx_complex_carrier_matrix, 1)*size(Rx_complex_carrier_matrix,2),1)' ;
%           
Rx_decoded_binary_symbols=demoduqam16(Rx_serial_complex_symbols);%  


%============================================================
baseband_in = Rx_decoded_binary_symbols;

figure(9);
subplot(2,1,1);
stem(baseband_out(1:100));
subplot(2,1,2);
stem(baseband_in(1:100));
%================     =============================================
bit_errors=find(baseband_in ~=baseband_out);
bit_error_count = size(bit_errors, 2) 
ber=bit_error_count/baseband_out_length


function [rcosw]=rcoswindow(beta, Ts)
%      ,  beta     ,Ts        OFDM     ,Ts    

t=0:(1+beta)*Ts;
rcosw=zeros(1,(1+beta)*Ts);
for i=1:beta*Ts;
rcosw(i)=0.5+0.5*cos(pi+ t(i)*pi/(beta*Ts));
end
rcosw(beta*Ts+1:Ts)=1;
for j=Ts+1:(1+beta)*Ts+1;
    rcosw(j-1)=0.5+0.5*cos((t(j)-Ts)*pi/(beta*Ts));
end
rcosw=rcosw';%      

    
function [complex_qam_data]=qam16(bitdata)
%modulation of 16QAM,modulate bitdata to 16QAM complex signal
X1=reshape(bitdata,4,length(bitdata)/4)';
d=1;%min distance of symble 
for i=1:length(bitdata)/4;
    for j=1:4
        X1(i,j)=X1(i,j)*(2^(4-j));
    end
        source(i,1)=1+sum(X1(i,:));%convert to the number 1 to 16
end
mapping=[-3*d 3*d;
	   -d  3*d;
        d  3*d;
	  3*d  3*d;
	 -3*d  d;
	   -d  d;
	    d  d;
	  3*d  d;
 	 -3*d  -d; 
	   -d  -d; 
	    d  -d;
      3*d  -d;
	 -3*d  -3*d;
	   -d  -3*d;
	    d  -3*d;
	  3*d  -3*d];
 for i=1:length(bitdata)/4
     qam_data(i,:)=mapping(source(i),:);%data mapping
 end
 complex_qam_data=complex(qam_data(:,1),qam_data(:,2));
 
function [demodu_bit_symble]=demoduqam16(Rx_serial_complex_symbols)
%      16QAM           
complex_symbols=reshape(Rx_serial_complex_symbols,length(Rx_serial_complex_symbols),1);
d=1;
mapping=[-3*d 3*d;
	   -d  3*d;
        d  3*d;
	  3*d  3*d;
	 -3*d  d;
	   -d  d;
	    d  d;
	  3*d  d;
 	 -3*d  -d; 
	   -d  -d; 
	    d  -d;
      3*d  -d;
	 -3*d  -3*d;
	   -d  -3*d;
	    d  -3*d;
	  3*d  -3*d];
  complex_mapping=complex(mapping(:,1),mapping(:,2));
  for i=1:length(Rx_serial_complex_symbols);
      for j=1:16;
          metrics(j)=abs(complex_symbols(i,1)-complex_mapping(j,1));
      end
      [min_metric  decode_symble(i)]= min(metrics) ;  %            decode_symble(i)
  end
  
  decode_bit_symble=de2bi((decode_symble-1)','left-msb');
   demodu_bit_symble=reshape(decode_bit_symble',1,length(Rx_serial_complex_symbols)*4);
      
 
function [complex_signal_matrix]=qpsk(baseband_out)
%modulation of QPSK,modulate bitdata to QPSK complex signal
convert_matrix = reshape(baseband_out',2,length(baseband_out)/2)' ;%    ,   2        
modulo_baseband = bi2de(convert_matrix,'left-msb');%2500*1                       10   
carrier_matrix = reshape(modulo_baseband,carrier_count,symbols_per_carrier)';%     -  25*100    100            25 OFDM  
carrier_matrix = [zeros(1,carrier_count); carrier_matrix];%              
for i = 2:(symbols_per_carrier + 1) %                
carrier_matrix(i,:) = rem(carrier_matrix(i,:) + carrier_matrix (i-1,:), 2^bits_per_symbol) ;%      (    )
end

carrier_matrix = carrier_matrix*((2*pi)/(2^bits_per_symbol));%     (0,0)=0 -->0;  (0,1)=1 -->90;  (1,0)=2-->180;     (1,1)=3  -->270
[X, Y]=pol2cart(carrier_matrix, ones(size(carrier_matrix,1),size(carrier_matrix,2))); %                                  1
complex_signal_matrix = complex(X, Y);

프로그램은 문제가 없습니다. 실행할 수 있습니다. 이 코드를 같은 폴더 아래에 두면 하위 파일 이름은 정의된 함수 이름과 일치해야 합니다. 단독 실행 함수는 결과가 없습니다. 첫 번째 프로그램만 실행하면 됩니다.

좋은 웹페이지 즐겨찾기