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

📄 a_filter_design.m

📁 OFDM_Final_Report.pdf后面附有matlab程序
💻 M
📖 第 1 页 / 共 4 页
字号:
% ch_noise.m
% ch_noise (operate on recv)
% random noise defined by noise_level amplitude
if already_made_noise == 0 % only generate once and use for both QAM and OFDM
noise = (rand(1,length(recv))-0.5)*2*noise_level;
already_made_noise = 1;
end
recv = recv + noise;
31
% ComputeChannelGUI.m
% ComputeChannelGUI.m plots the current channel
popupHnd1=findobj('Tag','PopupMenuMultipath');
noChannel = 0;
if get(popupHnd1,'Value') == 3 % Large
d1 = 6;
a1 = 0.4;
d2 = 10;
a2 = 0.3;
elseif get(popupHnd1,'Value') == 2 % Small
d1 = 6;
a1 = 0.25;
d2 = 10;
a2 = 0.20;
else % None
noChannel = 1;
channel_on = 0;
break
end
num = [1, zeros(1, d1-1), a1, zeros(1, d2-d1-1), a2];
den = [1];
[H, W] = freqz(num, den);
mag = 20*log10(abs(H));
phase = angle(H) * 180/pi;
% plot(W/(2*pi),mag) % comment me out normally
% eight2bin.m
function y = eight2bin(x)
% eight2bin
%
% Converts eight bit data (0-255 decimal) to a binary form for processing.
y = zeros(1,8);
k = 0;
while x > 0
y(8-k) = rem(x,2);
k = k+1;
x = floor(x/2);
end
% OFDM.m
% Run OFDM simulation
tic % Start stopwatch to calculate how long QAM simulation takes
disp(' '),disp('------------------------------------------------------------')
disp('OFDM Simulation')
tx
ch
rx
% Stop stopwatch to calculate how long QAM simulation takes
OFDM_simulation_time = toc;
if OFDM_simulation_time > 60
disp(strcat('Time for OFDM simulation=', num2str(OFDM_simulation_time/60), ' minutes.'));
else
disp(strcat('Time for OFDM simulation=', num2str(OFDM_simulation_time), ' seconds.'));
end
32
% OFDMguiFn.m
function OFDMguiFn(action)
% Consolidates all of the GUI callbacks into one main function
stringArray = [...
% Slide 1
'Welcome to the basic OFDM (Orthogonal Frequency '...
'Division Multiplexing) demo. Please click the Next '...
'button to get started. '...
' '...
' '...
' '...
' ';...
% Slide 2
'Assume that we want to transmit the following binary '...
'data using OFDM: [0 0 0 1 1 0 1 1]. '...
'The plot shows this binary data. '...
' '...
' '...
' '...
' ';...
% Slide 3
'In OFDM an IFFT (Inverse Fast Fourier Transform) is '...
'used to put the binary numbers onto many frequencies. '...
'Due to the math involved in an IFFT, these '...
'frequencies do not interfere with eath other (in '...
'communication terms, this is called "Orthogonality"). '...
'The plot shows that each group of 2 blue data points '...
'under a red hump will be put onto one frequency. ';...
% Slide 4
'The IFFT math is now complete. It has generated an '...
'OFDM signal that corresponds to the binary data. '...
'The plot shows the signal generated by the IFFT. '...
' '...
' '...
' '...
' ';...
% Slide 5 - same plot
'Now, this OFDM signal can be transmitted through a '...
'media and then received. This media (or "Channel" in '...
'communication) could be wired or wireless. Once the '...
'signal is received, the reverse process is done to '...
'recover the original binary data. '...
' '...
' ';...
% Slide 6
'Finally, an FFT (Fast Fourier Transform) is used to '...
'recover the binary data as shown in the plot. Note '...
'that the FFT is the opposite of the IFFT used to '...
'generate the OFDM signal. As long as the Channel does '...
'not distort the OFDM signal too much, the original '...
'binary data can be recovered. '...
' '];
switch(action)
case 'next' %---------------------------------------
textHnd1=findobj('Tag','StaticTextFeedback');
nextHnd1=findobj('Tag','PushbuttonNext'); % handler for the Next button
axisHnd1=findobj('Tag','Axes1');
global COUNTER
if isempty(COUNTER)
COUNTER = 0; % initialize COUNTER if doesn't exist
end
COUNTER = COUNTER + 1;
[r c]=size(stringArray);
if COUNTER > r
COUNTER = 0;
close(gcf)
basicGUI % set to file name in future!
else
set(textHnd1,'String',stringArray(COUNTER,:))
switch(COUNTER)
case 1
% disp('Slide 1')
case 2
% disp('Slide 2')
33
setupGUI % sets up the GUI variables
set(axisHnd1,'Visible','on')
% Stem Plot the Binary Data
stem(data_in,'filled')
case 3
% disp('Slide 3')
setupGUI % sets up the GUI variables
% add groupings around the stem plot
y=1.2*abs(sin(linspace(0,4*pi,80))).^(1/5);
x=linspace(0.5,8.5,80);
plot(x,y,'r'),hold on
stem(data_in,'filled'),hold off
case 4
% disp('Slide 4')
setupGUI
% Perform the ifft and display the results
tx
plot(xmit)
case 5
% disp('Slide 5')
% same plot
case 6
% disp('Slide 6')
setupGUI
tx, ch, rx
stem(output,'filled')
set(nextHnd1,'String','Start Over') % repeat if desired
otherwise
disp('error')
end
end
case 'close' %---------------------------------------
clear global COUNTER
close(gcbf)
case 'figure' %---------------------------------------
% this is called whenever the figure is first created -or NOT???
textHnd1=findobj('Tag','StaticTextFeedback');
axisHnd1=findobj('Tag','Axes1');
set(textHnd1,'String','Basic OFDM Demo') % default text message
set(axisHnd1,'Visible','off') % hide Axis to begin
end
% OFDMguiFnSound.m
function OFDMguiFnSound(action)
% Consolidates all of the GUI callbacks into one main function
% Alan Brooks the man wrote this
stringArray = [...
% Slide 1
'Welcome to the Sound OFDM demo. This simulates QAM '...
'and OFDM using a sound file as input to demonstrate '...
'the advantages of using OFDM with a multipath '...
'channel. '...
'Choose the strength of multipath present in the '...
'channel and the plot will show the current channels '...
'frequency response. ';...
% Slide 2
'Here is a frequency domain (FD) representation of the '...
'QAM data to be transmitted. '...
'Press any key to continue. '...
' '...
' '...
' '...
' ';...
% Slide 2b
'For QAM (single-carrier) transmission, this plot '...
'shows the channel frequency response (black) and the '...
'received data (light blue) overlayed on the original '...
'data (blue). Note that the received data is slightly '...
'distorted due to the fading channel caused by '...
'multipath. '...
'Press any key to continue. ';...
% Slide 2c
'Here is a frequency domain (FD) representation of the '...
34
'OFDM data to be transmitted. '...
'Press any key to continue. '...
' '...
' '...
' '...
' ';...
% Slide 2d
'For OFDM (multi-carrier) transmission, this plot '...
'shows the channel (black) and received data (light '...
'blue) overlayed on the original data (blue). Note '...
'that the OFDM received data also exhibits multipath '...
'distortion. Also, notice that the OFDM signal is '...
'spread out over more bandwidth than QAM since OFDM '...
'uses many carrier frequencies. ';...
% Slide 3
'Here are the final plots of the recovered sound files '...
'along with the Bit Error Rate (BER) for OFDM and QAM. '...
'Click any of the 3 buttons to hear these sounds. '...
'Since OFDM handles multipath better, the sound is '...
'less distorted. '...
'The Long Sounds demonstrate longer examples that have '...
'already been processed offline. '];
switch(action)
case 'next' %---------------------------------------
textHnd1=findobj('Tag','StaticTextFeedback');
nextHnd1=findobj('Tag','PushbuttonNext'); % handler for the Next button
% axis handlers
axisHnd1=findobj('Tag','Axes1'); % main
axisHnd2=findobj('Tag','AxesOriginal'); % original
axisHnd3=findobj('Tag','AxesQAM'); % QAM
axisHnd4=findobj('Tag','AxesOFDM'); % OFDM
% multipath handlers
textHnd2=findobj('Tag','StaticTextMultipath');
popupHnd1=findobj('Tag','PopupMenuMultipath');
% Generated Sounds handlers
textHnd3=findobj('Tag','StaticTextGenSounds');
OriginalHnd1=findobj('Tag','PushbuttonOriginal');
QAMHnd1=findobj('Tag','PushbuttonQAM');
OFDMHnd1=findobj('Tag','PushbuttonOFDM');
% Long Sounds handlers
textHnd4=findobj('Tag','StaticTextLongSounds');
OriginalLongHnd1=findobj('Tag','PushbuttonOriginalLong');
QAMLongHnd1=findobj('Tag','PushbuttonQAMLong');
OFDMLongHnd1=findobj('Tag','PushbuttonOFDMLong');
% BER handlers
textHnd5=findobj('Tag','StaticTextBER1'); % label
textHnd6=findobj('Tag','StaticTextBER2'); % label
textHnd7=findobj('Tag','StaticTextBERQAM'); % OFDM BER field
textHnd8=findobj('Tag','StaticTextBEROFDM'); % QAM BER field
global COUNTER
if isempty(COUNTER)
COUNTER = 0; % initialize COUNTER if doesn't exist
end
COUNTER = COUNTER + 1;
[r c]=size(stringArray);
if COUNTER > r
COUNTER = 0;
close(gcf)
SoundGUI % set to file name in future!
else
set(textHnd1,'String',stringArray(COUNTER,:))
switch(COUNTER)
case 1
% disp('Slide 1')
% Show/Hide the GUI
set(nextHnd1,'String','Next')
% show multipath controls
set(textHnd2,'Visible','on')
set(popupHnd1,'Visible','on')
% enable multipath controls
set(textHnd2,'Enable','on')
set(popupHnd1,'Enable','on')
% show main axis
set(axisHnd1,'Visible','on'),axes(axisHnd1)
% hide other axis's
set(axisHnd2,'Visible','off')
35
set(axisHnd3,'Visible','off')
set(axisHnd4,'Visible','off')
% hide generated sounds stuff
set(textHnd3,'Visible','off')
set(OriginalHnd1,'Visible','off')
set(QAMHnd1,'Visible','off')
set(OFDMHnd1,'Visible','off')
% hide long sounds stuff
set(textHnd4,'Visible','off')
set(OriginalLongHnd1,'Visible','off')
set(QAMLongHnd1,'Visible','off')
set(OFDMLongHnd1,'Visible','off')
% hide the BER displays
set(textHnd5,'Visible','off')
set(textHnd6,'Visible','off')
set(textHnd7,'Visible','off')
set(textHnd8,'Visible','off')
set(popupHnd1,'Value',1) % no channel by default
% default plot
plot(0:.05:.5,zeros(1,11)),axis([0 0.5 -12 6]),title('Channel Magnitude
Response')
xlabel('Digital Frequency'),ylabel('Magnitude (dB)')
case {2, 3, 4, 5}
% disp('Slide 2')
% disble multipath controls
set(textHnd2,'Enable','off')
set(popupHnd1,'Enable','off')
setupSoundGUI % sets up the Sound GUI variables
set(textHnd1,'String','QAM Simulation... Please Wait')
QAM
set(textHnd1,'String',stringArray(COUNTER,:))
fft_temp = abs(fft(QAM_tx_data));
fft_temp = fft_temp(1:floor(0.5*length(fft_temp))); % truncate (+ spectrum)
dig_x_axis = (1:length(fft_temp)) / (2*length(fft_temp));
plot(dig_x_axis, fft_temp)
title('FFT of Transmitted QAM')
% calculate the BER and store for slide 6
global BER_QAM_TEMP;
binary_err_bits_QAM = 0;
for i = 1:length(data_in)
err = abs(data_in(i)-QAM_data_out(i));
if err > 0
binary_err_bits_QAM = binary_err_bits_QAM + 1;
end
end
BER_QAM_TEMP = 100 * binary_err_bits_QAM/data_length;
COUNTER = COUNTER + 1;
pause
% disp('Slide 2b')
set(textHnd1,'String',stringArray(COUNTER,:))
hold on
% QAM Plotting
fft_temp = abs(fft(QAM_rx_data));
fft_temp = fft_temp(1:floor(0.5*length(fft_temp))); % truncate
plot(dig_x_axis, fft_temp,'c'),title(' ')
% channel display
if channel_on == 1
ComputeChannelGUI
size_mag=max(mag)-min(mag); % for scaled channel plot
plot(W/(2*pi),(0.5*max(fft_temp)/size_mag)*(mag +
abs(min(mag))) + 0.5*max(fft_temp),'k')
end
hold off
COUNTER = COUNTER + 1;
pause
% disp('Slide 2c')
set(textHnd1,'String','OFDM Simulation... Please Wait')
OFDM
set(textHnd1,'String',stringArray(COUNTER,:))
fft_temp = abs(fft(xmit));
fft_temp = fft_temp(1:floor(0.5*length(fft_temp))); % truncate
dig_x_axis = (1:length(fft_temp)) / (2*length(fft_temp));
plot(dig_x_axis, fft_temp)
title('FFT of Transmitted OFDM')
% calculate the BER and store for slide 6
global BER_OFDM_TEMP;
binary_err_bits_OFDM = 0;
36
for i = 1:length(data_in)
err = abs(data_in(i)-output(i));
if err > 0
binary_err_bits_OFDM = binary_err_bits_OFDM +1;
end
end
BER_OFDM_TEMP = 100 * binary_err_bits_OFDM/data_length;
COUNTER = COUNTER + 1;
pause
% disp('Slide 2d')
set(textHnd1,'String',stringArray(COUNTER,:))
hold on
% OFDM Plotting
fft_temp = abs(fft(recv));
fft_temp = fft_temp(1:floor(0.5*length(fft_temp))); % truncate
plot(dig_x_axis, fft_temp,'c'),title(' ')
% channel display
if channel_on == 1
plot(W/(2*pi),(0.5*max(fft_temp)/size_mag)*(mag +
abs(min(mag))) + 0.5*max(fft_temp),'k')
end
hold off
case 6
% disp('Slide 3')
setupSoundGUI
% hide main axis
plot(0) % clear the plot
axis off
% set(axisHnd1,'Visible','off')
% show other axis's
set(axisHnd2,'Visible','on')
set(axisHnd3,'Visible','on')
set(axisHnd4,'Visible','on')
% hide multipath controls
set(textHnd2,'Visible','off')
set(popupHnd1,'Visible','off')
% show generated sound buttons
set(textHnd3,'Visible','on')
set(OriginalHnd1,'Visible','on')
set(QAMHnd1,'Visible','on')
set(OFDMHnd1,'Visible','on')
% show long sounds stuff
set(textHnd4,'Visible','on')
set(OriginalLongHnd1,'Visible','on')
set(QAMLongHnd1,'Visible','on')
set(OFDMLongHnd1,'Visible','on')
% show the BER displays
set(textHnd5,'Visible','on')
set(textHnd6,'Visible','on')
set(textHnd7,'Visible','on') % QAM
set(textHnd8,'Visible','on') % OFDM
% Display the BERs
global BER_QAM_TEMP;
global BER_OFDM_TEMP;
set(textHnd7,'String',strcat(num2str(BER_QAM_TEMP,3),' %'))
set(textHnd8,'String',strcat(num2str(BER_OFDM_TEMP,3),' %'))
clear global BER_QAM_TEMP; % clean up the globals
clear global BER_OFDM_TEMP;
% Plot the Sounds
% Note: axes(handle) sets to plot on the handle axis
axes(axisHnd2)
plot(wavread(file_name)),title('Original sound')
axes(axisHnd3)
plot(wavread('QAM_out.wav')),title('QAM sound')
axes(axisHnd4)
plot(wavread('OFDM_out.wav')),title('OFDM sound')
set(nextHnd1,'String','Start Over') % repeat if desired
otherwise
disp('error')
COUNTER = 0;
end
end
case 'mp_channel' %-----------------------------------
ComputeChannelGUI

⌨️ 快捷键说明

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