7
% Faculty of Engineering, University of Fukui. 2003/11/11 % coded by Sokthai Chan ([email protected]) % OFDM signal and its spectrum ( Guard Interval insertion ) clear all; OFDM signal Fd=1; % symbol rate (1Hz) Fs=1*Fd; % number of sample per symbol M=4; % kind(range) of symbol (0,1,2,3) Ndata=1024; % all transmitted data symbol Sdata=64; % 64 data symbol per frame to ifft Slen=128; % 128 length symbol for IFFT Nsym=Ndata/Sdata; % number of frame -> Nsym frame GIlen=144; % symbol with GI insertion GI=16; % guard interval length % vector initialization X=zeros(Ndata,1); Y1=zeros(Ndata,1); Y2=zeros(Ndata,1); Y3=zeros(Slen,1); z0=zeros(Slen,1); z1=zeros(Ndata/Sdata*Slen,1); g=zeros(GIlen,1); z2=zeros(GIlen*Nsym,1); z3=zeros(GIlen*Nsym,1); % random integer generation by M kinds X = randint(Ndata, 1, M); % digital symbol mapped as analog symbol Y1 = modmap(X, Fd, Fs, 'qask', M); % covert to complex number Y2=amodce(Y1,1,'qam'); for j=1:Nsym; for i=1:Sdata; Y3(i+Slen/2-Sdata/2,1)=Y2(i+(j-1)*Sdata,1); end z0=ifft(Y3); for i=1:Slen; z1(((j-1)*Slen)+i)=z0(i,1); end % for i=1:Slen; g(i+16)=z0(i,1); end

Faculty of Engineering

  • Upload
    hau-le

  • View
    19

  • Download
    10

Embed Size (px)

Citation preview

Page 1: Faculty of Engineering

% Faculty of Engineering, University of Fukui. 2003/11/11 % coded by Sokthai Chan ([email protected]) % OFDM signal and its spectrum ( Guard Interval insertion )

clear all; OFDM signal

Fd=1; % symbol rate (1Hz) Fs=1*Fd; % number of sample per symbol M=4; % kind(range) of symbol (0,1,2,3)

Ndata=1024; % all transmitted data symbol Sdata=64; % 64 data symbol per frame to ifft Slen=128; % 128 length symbol for IFFT Nsym=Ndata/Sdata; % number of frame -> Nsym frame GIlen=144; % symbol with GI insertion GI=16; % guard interval length

% vector initialization X=zeros(Ndata,1); Y1=zeros(Ndata,1); Y2=zeros(Ndata,1); Y3=zeros(Slen,1); z0=zeros(Slen,1); z1=zeros(Ndata/Sdata*Slen,1); g=zeros(GIlen,1); z2=zeros(GIlen*Nsym,1); z3=zeros(GIlen*Nsym,1);

% random integer generation by M kinds X = randint(Ndata, 1, M);

% digital symbol mapped as analog symbol Y1 = modmap(X, Fd, Fs, 'qask', M);

% covert to complex number Y2=amodce(Y1,1,'qam');

for j=1:Nsym;

for i=1:Sdata; Y3(i+Slen/2-Sdata/2,1)=Y2(i+(j-1)*Sdata,1); end

z0=ifft(Y3);

for i=1:Slen; z1(((j-1)*Slen)+i)=z0(i,1); end

% for i=1:Slen; g(i+16)=z0(i,1); end

Page 2: Faculty of Engineering

for i=1:GI; g(i)=z0(i+Slen-GI,1); end

for i=1:GIlen; z2(((j-1)*GIlen)+i)=g(i,1); end

end

% graph on time domain figure(1); f = linspace(-Sdata,Sdata,length(z1)); plot(f,abs(z1));

Y4 = fft(z1);

% if Y4 is under 0.01 Y4=0.001 for j=1:Ndata/Sdata*Slen; if abs(Y4(j)) < 0.01 Y4(j)=0.01; end end

Y4 = 10*log10(abs(Y4));

% graph on frequency domain figure(2); f = linspace(-Sdata,Sdata,length(Y4)); plot(f,Y4); axis([-Slen/2 Slen/2 -20 20]);

QAM

% QAM.m So sanh OFDM (Da song mang) voi QAM da muc (Don song mang) % Khi chung ta phat cung mot so luong bit giong ngau tren mot chu ky thoi % gian

read % Doc du lieu cho QAM - Khong anh huong den OFDM data_in_pol = bin2pol(data_in); % Chuyen doi du lieu nhi phan thanh du

lieu phan cuc

% Kiem tra so song mang co phai la luy thua cua 2 is_pow_2 = num_carriers; temp_do_QAM = 0; if is_pow_2 ~= 2 while temp_do_QAM == 0 temp_do_QAM = rem(is_pow_2,2); is_pow_2 = is_pow_2/2; if is_pow_2 == 2 temp_do_QAM = -99; % Dung la luy thua cua 2 -> Co the thuc

hien QAM

Page 3: Faculty of Engineering

end end else temp_do_QAM = -99; % 2 la luy thua cua 2 end if temp_do_QAM ~= -99 do_QAM = 0; % Khong the thuc hien disp(' '),disp('ERROR: Cannot run QAM because num_carriers is not

valid.') disp(' Please see "setup.m" for details.') end

if do_QAM == 1 tic % Bat dau de tinh toan thoi gian mo phong thuc hien QAM

disp(' '), disp('--------------------------------------------------------

----') disp('QAM simulation'), disp('Transmitting')

% Them cac muc zeros de du lieu duoc chia thanh cac phan bang nhau data_length = length(data_in_pol); r = rem(data_length,num_carriers); if r ~= 0 for i = 1:num_carriers-r data_in_pol(data_length+i) = 0; %Them dau vao voi cac zeros vao

tap hop du lieu end %Toc do co the duoc cai thien end data_length = length(data_in_pol); %Cap nhat sau khi them

num_OFDM_symbols = ceil(data_length / (2*num_carriers)); % So ky hieu QAM duoc bieu dien bang so luong cua du lieu tren mot ky % hieu OFDM num_QAM_symbols = num_carriers / 2; % So mau tren ky hieu QAM num_symbol_samples = fft_size / num_QAM_symbols;

% Chuyen doi du lieu phan cuc [-1, 1] thanh du lieu 4 muc [-3, -1, 1, 3] data_in_4 = zeros(1,data_length/2); for i = 1:2:data_length data_in_4(i - (i-1)/2) = data_in_pol(i)*2 + data_in_pol(i+1); end

% Dinh ro diem lay mau giua 0 va 2*pi ts = linspace(0, 2*pi*QAM_periods, num_symbol_samples+1);

% Phat du lieu 16-QAM % Tong do dai cua truyen dan 16-QAM tx_length = num_OFDM_symbols * num_QAM_symbols * num_symbol_samples; QAM_tx_data = zeros(1,tx_length); for i = 1:2:data_length/2 for k = 1:num_symbol_samples 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));

Page 4: Faculty of Engineering

end end

% Do channel simulation on QAM data xmit = QAM_tx_data; % ch dung du lieu 'xmit' va tra ve 'recv' ch QAM_rx_data = recv; % Luu du lieu QAM sau khi mo phong kenh clear recv %Loai bo 'recv' sao cho no khong nhieu voi OFDM clear xmit % Loai bo 'xmit' sao cho no khong nhieu voi OFDM

disp('Receiving') % Khoi phuc du lieu nhi phan (Giai ma QAM) cos_temp = zeros(1,num_symbol_samples); % sin_temp = cos_temp; % xxx = zeros(1,data_length/4); % Khoi tao muc khong cho toc

do yyy = xxx; % QAM_data_out_4 = zeros(1,data_length/2); %

for i = 1:2:data_length/2 % "cheating" for k = 1:num_symbol_samples % Tang so song mang de tao tan so cao va du lieu goc cos_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) *

cos(ts(k)); sin_temp(k) = QAM_rx_data(k+((i-1)/2)*num_symbol_samples) *

sin(ts(k)); end % LPF va xac dinh - chung ta se rat don gian LPF bang phep trung % binh xxx(1+(i-1)/2) = mean(cos_temp); yyy(1+(i-1)/2) = mean(sin_temp); % Khoi phuc du lieu thanh dang noi tiep QAM_data_out_4(i) = xxx(1+(i-1)/2); QAM_data_out_4(i+1) = yyy(1+(i-1)/2); end

% Thuc hien xac dinh giua cac muc [-3, -1, 1, 3] for i = 1:data_length/2 if QAM_data_out_4(i) >= 1, QAM_data_out_4(i) = 3; elseif QAM_data_out_4(i) >= 0, QAM_data_out_4(i) = 1; elseif QAM_data_out_4(i) >= -1, QAM_data_out_4(i) = -1; else QAM_data_out_4(i) = -3; end end

% Chuyen doi du lieu 4 muc [-3, -1, 1, 3] ve du lieu phan cuc [-1, 1] QAM_data_out_pol = zeros(1,data_length); % "cheating" for i = 1:2:data_length switch QAM_data_out_4(1 + (i-1)/2) case -3 QAM_data_out_pol(i) = -1; QAM_data_out_pol(i+1) = -1; case -1 QAM_data_out_pol(i) = -1; QAM_data_out_pol(i+1) = 1; case 1 QAM_data_out_pol(i) = 1;

Page 5: Faculty of Engineering

QAM_data_out_pol(i+1) = -1; case 3 QAM_data_out_pol(i) = 1; QAM_data_out_pol(i+1) = 1; otherwise disp('Error detected in switch statment - This should not be

happening.'); end end QAM_data_out = pol2bin(QAM_data_out_pol); % Chuyen doi ve du lieu nhi

phan

% Ngung tinh toan thoi gian thuc hien mo phong QAM QAM_simulation_time = toc; if QAM_simulation_time > 60 disp(strcat('Time for QAM simulation=',

num2str(QAM_simulation_time/60), ' minutes.')); else disp(strcat('Time for QAM simulation=', num2str(QAM_simulation_time),

' seconds.')); end end

So sanh am thanh input_type = 2; file_input_type = 3; file_name = 'shortest.wav'; channel_on = 1; do_QAM = 1; QAM_periods = 10; clip_level = 1.0; % 0.0 - 1.0 (0-100%) noise_level = 0.0; already_made_noise = 0;

% --- Executes just before Sosanhamthanh is made visible. function Sosanhamthanh_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to Sosanhamthanh (see VARARGIN)

% Choose default command line output for Sosanhamthanh handles.output = hObject;

% Update handles structure guidata(hObject, handles);

% UIWAIT makes Sosanhamthanh wait for user response (see UIRESUME) % uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line. function varargout = Sosanhamthanh_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB

Page 6: Faculty of Engineering

% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure varargout{1} = handles.output;

% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) popup_sel_index = get(handles.Chonam, 'Value'); switch popup_sel_index case 1 axes(handles.Amthanhbandau_axes) plot(wavread('Long.wav')),title('Am thanh phat') grid on sound(wavread('Long.wav'),11025) end

% --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) popup_sel_index = get(handles.Chonam, 'Value'); switch popup_sel_index case 1 axes(handles.QAM_axes) plot(wavread('QAM_Long.wav')),title('Am duoc dieu che bang phuong thuc QAM') grid on sound(wavread('QAM_Long.wav'),11025) end

% --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) popup_sel_index = get(handles.Chonam, 'Value'); switch popup_sel_index case 1 axes(handles.OFDM_axes) plot(wavread('OFDM_Long.wav')),title('Am duoc dieu che bang phuong thuc

OFDM') grid on sound(wavread('OFDM_Long.wav'),11025) end

% --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

Page 7: Faculty of Engineering

% --- Executes during object creation, after setting all properties. function Chonam_CreateFcn(hObject, eventdata, handles) % hObject handle to Chonam (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end

% --- Executes on selection change in Chonam. function Chonam_Callback(hObject, eventdata, handles) % hObject handle to Chonam (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)