📄 a_filter_design.m
字号:
Orthogonal Frequency Division Multiplexing (OFDM)
Alan C. Brooks & Steve J. Hoelzer
5/1/2001 Code Freeze
Code Statistics: 1,969 lines; 6,635 words; 46,742 characters (omitting spaces)
% a_filter_design.m
% Design filter by specifying delay in units and
% looking at mag and phase response
% Good default values for fft_size = 128 and num_carriers = 32
delay_1 = 6; % 6
attenuation_1 = 0.35; % 0.35
delay_2 = 10; % 10
attenuation_2 = 0.30; % 0.30
num = [1, zeros(1, delay_1-1), attenuation_1, zeros(1, delay_2-delay_1-1), attenuation_2];
[H, W] = freqz(num, 1, 512); % compute frequency response
mag = 20*log10(abs(H)); % magnitude in dB
phase = angle(H) * 180/pi; % phase angle in degrees
figure(9), clf
subplot(211), plot(W/(2*pi),mag)
title('Magnitude response of multipath channel')
xlabel('Digital Frequency'), ylabel('Magnitude in dB')
subplot(212), plot(W/(2*pi),phase)
title('Phase response of multipath channel')
xlabel('Digital Frequency'), ylabel('Phase in Degrees')
break
% Design filter using MATLAB command 'fir2'
nn = 40; % order of filter
f = [0, 0.212, 0.253, 0.293, 0.5];
m =[1, 1, 0.5, 1, 1];
num = fir2(nn, 2*f, m);
den = 1;
[H, W] = freqz(num, den, 256); % Compute freq response
mag = 20*log10(abs(H)); % Get mag in dB
phase = angle(H)*180/pi; % Get phase in degrees
clf
subplot(211), plot(W/(2*pi),mag)
subplot(212), plot(W/(2*pi),phase)
break
% Design filter using MATLAB command 'fir1'
% These coeffs work well for OFDM vs. QAM!!!
% nn = 4; % order of filter
% wl = 0.134; % low cutoff of stopband
% wh = 0.378; % high cutoff of stopband
% nn = 4; % order of filter
% wl = 0.195; % low cutoff of stopband
% wh = 0.309; % high cutoff of stopband
nn = 8; % order of filter
wl = 0.134; % low cutoff of stopband
wh = 0.378; % high cutoff of stopband
num = fir1(nn, 2*[wl, wh], 'stop');
den = 1;
[H, W] = freqz(num, den, 256); % Compute freq response
mag = 20*log10(abs(H)); % Get mag in dB
phase = angle(H)*180/pi; % Get phase in degrees
clf
subplot(211), plot(W,mag), hold on, plot(wl*2*pi,0,'o'), plot(wh*2*pi,0,'o')
subplot(212), plot(W,phase), hold on, plot(wl*2*pi,0,'o'), plot(wh*2*pi,0,'o')
hold off
break
% Design filter by specifying delay in units and looking at mag and phase response
n = 512;
d1 =4;
a1 = 0.2;
25
d2 = 5;
a2 = 0.3;
num = [1, zeros(1, d1-1), a1, zeros(1, d2-d1-1), a2]
den = [1];
[H, W] = freqz(num, den, n);
% F = 0:.1:pi;
% H = freqz(num, den, F*180/pi, 11025);
mag = 20*log10(abs(H));
% phase = angle(H * 180/pi);
phase = angle(H);
clf
subplot(211), plot(W,mag), hold on, plot(0.17*pi,0,'o'), plot(0.34*pi,0,'o')
subplot(212), plot(W,phase), hold on, plot(pi/2,0,'o')
hold off
break
% Design filter by specifying mag response at particular frequencies
n = 2;
f = [0, 0.25, 0.5];
mag = [1, .05, 1];
[num, den] = yulewalk(n,2*f,mag);
[H, W] = freqz(num, den);
mag = 20*log10(abs(H));
phase = angle(H * 180/pi);
clf
subplot(211), plot(W,mag)
subplot(212), plot(W,phase)
% a_run_demo.m
setup
QAM
OFDM
analysis
% analysis.m
% Analysis
disp(' '), disp('------------------------------------------------------------')
disp('Preparing Analysis')
figure(1), clf
if (input_type == 1) & (test_input_type == 1)
subplot(221), stem(data_in), title('OFDM Binary Input Data');
subplot(223), stem(output), title('OFDM Recovered Binary Data')
else
subplot(221), plot(data_samples), title('OFDM Symbol Input Data');
subplot(223), plot(output_samples), title('OFDM Recovered Symbols');
end
subplot(222), plot(xmit), title('Transmitted OFDM');
subplot(224), plot(recv), title('Received OFDM');
% dig_x_axis = (1:length(QAM_tx_data))/length(QAM_tx_data);
% figure(4), clf, subplot(212)
% freq_data = abs(fft(QAM_rx_data));
% L = length(freq_data)/2;
dig_x_axis = (1:length(xmit))/length(xmit);
figure(2), clf
if channel_on ==1
num = [1, zeros(1, d1-1), a1, zeros(1, d2-d1-1), a2];
den = [1];
[H, W] = freqz(num, den, 512);
26
mag = 20*log10(abs(H));
phase = angle(H) * 180/pi;
subplot(313)
freq_data = abs(fft(recv));
L = length(freq_data)/2;
plot(dig_x_axis(1:L), freq_data(1:L))
xlabel('FFT of Received OFDM')
axis_temp = axis;
subplot(311),
freq_data = abs(fft(xmit));
plot(dig_x_axis(1:L), freq_data(1:L)), axis(axis_temp)
title('FFT of Transmitted OFDM')
subplot(312)
plot(W/(2*pi),mag),
ylabel('Channel Magnitude Response')
else
subplot(212)
freq_data = abs(fft(recv));
L = length(freq_data)/2;
plot(dig_x_axis(1:L), freq_data(1:L))
xlabel('FFT of Received OFDM')
axis_temp = axis;
subplot(211),
freq_data = abs(fft(xmit));
plot(dig_x_axis(1:L), freq_data(1:L)), axis(axis_temp)
title('FFT of Transmitted OFDM')
end
% if file_input_type == 4
% figure(5)
% subplot(211)
% image(data_in);
% colormap(map);
% subplot(212)
% image(output);
% colormap(map);
% end
if do_QAM == 1 % analyze if QAM was done
figure(3), clf
if (input_type == 1) & (test_input_type == 1)
subplot(221), stem(data_in), title('QAM Binary Input Data');
subplot(223), stem(QAM_data_out), title('QAM Recovered Binary Data')
else
subplot(221), plot(data_samples), title('QAM Symbol Input Data');
subplot(223), plot(QAM_output_samples), title('QAM Recovered Symbols');
end
subplot(222), plot(QAM_tx_data), title('Transmitted QAM');
subplot(224), plot(QAM_rx_data), title('Received QAM');
dig_x_axis = (1:length(QAM_tx_data))/length(QAM_tx_data);
figure(4), clf
if channel_on ==1
subplot(313)
freq_data = abs(fft(QAM_rx_data));
L = length(freq_data)/2;
plot(dig_x_axis(1:L), freq_data(1:L))
xlabel('FFT of Received QAM')
axis_temp = axis;
subplot(311),
freq_data = abs(fft(QAM_tx_data));
plot(dig_x_axis(1:L),freq_data(1:L)), axis(axis_temp)
title('FFT of Transmitted QAM')
subplot(312)
plot(W/(2*pi),mag)
ylabel('Channel Magnitude Response')
else
subplot(212)
freq_data = abs(fft(QAM_rx_data));
L = length(freq_data)/2;
plot(dig_x_axis(1:L), freq_data(1:L))
title('FFT of Received QAM')
27
axis_temp = axis;
subplot(211),
freq_data = abs(fft(QAM_tx_data));
plot(dig_x_axis(1:L),freq_data(1:L)), axis(axis_temp)
title('FFT of Transmitted QAM')
end
% Plots the QAM Received Signal Constellation
figure(5), clf, plot(xxx,yyy,'ro'), grid on, axis([-2.5 2.5 -2.5 2.5]), hold on
% % Overlay plot of transmitted constellation
% x_const = [-1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5];
% y_const = [-1.5 -1.5 -1.5 -1.5 -0.5 -0.5 -0.5 -0.5 0.5 0.5 0.5 0.5 1.5 1.5 1.5 1.5];
% plot(x_const, y_const, 'b*')
% Overlay of constellation boundarys
x1 = [-2 -2]; x2 = [-1 -1]; x3 = [0 0]; x4 = [1 1]; x5 = [2 2]; x6 = [-2 2];
y1 = [-2 -2]; y2 = [-1 -1]; y3 = [0 0]; y4 = [1 1]; y5 = [2 2]; y6 = [-2 2];
plot(x1,y6), plot(x2,y6), plot(x3,y6), plot(x4,y6), plot(x5,y6)
plot(x6,y1), plot(x6,y2), plot(x6,y3), plot(x6,y4), plot(x6,y5)
hold off
title('16-QAM Received Signal Constellation and Decision Boundarys')
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 = 100 * binary_err_bits_QAM/data_length;
end
figure(6), clf
if channel_on == 1
subplot(211), plot(W/(2*pi),mag),title('Channel Magnitude Response')
xlabel('Digital Frequency'),ylabel('Magnitude in dB')
subplot(212), plot(W/(2*pi),phase),title('Channel Phase Response')
xlabel('Digital Frequency'),ylabel('Phase in Degrees')
else
title('Channel is turned off - No frequency response to plot')
end
% Compare output to input and count errors
binary_err_bits_OFDM = 0;
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 = 100 * binary_err_bits_OFDM/data_length;
disp(strcat('OFDM: BER=', num2str(BER_OFDM,3), ' %'))
disp(strcat(' Number of error bits=', num2str(binary_err_bits_OFDM)))
if (do_QAM == 1)
disp(strcat('QAM: BER=', num2str(BER_QAM,3), ' %'))
disp(strcat(' Number of error bits=', num2str(binary_err_bits_QAM)))
end
% Display text file before and after modulation
if (input_type == 2) & (file_input_type == 2)
original_text_file = char(data_samples')
if do_QAM ==1
edit QAM_text_out.txt
end
edit OFDM_text_out.txt
end
% Listen to sounds
if (input_type == 2) & (file_input_type == 3)
do_again = '1';
while ( ~(isempty(do_again)) )
disp(' ')
disp('Press any key to hear the original sound'), %pause
sound(data_samples,11025)
disp('Press any key to hear the sound after OFDM transmission'), %pause
sound(output_samples,11025)
28
if do_QAM == 1
disp('Press any key to hear the sound after QAM transmission'), %pause
sound(QAM_output_samples,11025)
end
do_again = '';
do_again = input('Enter "1" to hear the sounds again or press "Return" to end ', 's');
end
end
% BasicGUI.m
function BasicGUI()
% This is the machine-generated representation of a MATLAB object
% and its children. Note that handle values may change when these
% objects are re-created. This may cause problems with some callbacks.
% The command syntax may be supported in the future, but is currently
% incomplete and subject to change.
%
% To re-open this system, just type the name of the m-file at the MATLAB
% prompt. The M-file and its associtated MAT-file must be on your path.
load BasicGUI
a = figure('Color',[0.8 0.8 0.8], ...
'Colormap',mat0, ...
'CreateFcn','OFDMguiFn figure', ...
'Position',[490 321 512 384], ...
'Resize','off', ...
'Tag','Fig1');
b = uicontrol('Parent',a, ...
'Units','points', ...
'BackgroundColor',[1 1 1], ...
'FontName','Monaco', ...
'HorizontalAlignment','left', ...
'Position',[8 5 340 94], ...
'String','Basic OFDM Demo', ...
'Style','text', ...
'Tag','StaticTextFeedback');
b = uicontrol('Parent',a, ...
'Units','points', ...
'BackgroundColor',[0.3 0.3 0.3], ...
'Position',[367 0 147 387], ...
'Style','frame', ...
'Tag','Frame1');
b = uicontrol('Parent',a, ...
'Units','points', ...
'BackgroundColor',[0.733333 0.733333 0.733333], ...
'Callback','OFDMguiFn next', ...
'FontSize',14, ...
'Position',[379 340 102 32], ...
'String','Next', ...
'Tag','PushbuttonNext');
b = uicontrol('Parent',a, ...
'Units','points', ...
'BackgroundColor',[0.733333 0.733333 0.733333], ...
'Callback','OFDMguiFn close', ...
'FontSize',14, ...
'Position',[379 11 102 32], ...
'String','Close', ...
'Tag','PushbuttonClose');
b = uicontrol('Parent',a, ...
'Units','points', ...
'BackgroundColor',[0.733333 0.733333 0.733333], ...
'Position',[379 248 58 26], ...
'String',mat1, ...
'Style','popupmenu', ...
'Tag','PopupMenu1', ...
'Value',2, ...
'Visible','off');
b = uicontrol('Parent',a, ...
'Units','points', ...
'BackgroundColor',[0.733333 0.733333 0.733333], ...
'FontWeight','bold', ...
'Position',[379 283 129 17], ...
'String','Number of Carriers', ...
'Style','text', ...
'Tag','StaticText2', ...
'Visible','off');
b = axes('Parent',a, ...
29
'Units','points', ...
'CameraUpVector',[0 1 0], ...
'CameraUpVectorMode','manual', ...
'Color',[1 1 1], ...
'ColorOrder',mat2, ...
'Position',[44 130 294 235], ...
'Tag','AxesMain', ...
'Visible','off', ...
'XColor',[0 0 0], ...
'YColor',[0 0 0], ...
'ZColor',[0 0 0]);
c = text('Parent',b, ...
'Color',[0 0 0], ...
'HandleVisibility','callback', ...
'HorizontalAlignment','center', ...
'Position',[0.5 -0.0662393 0], ...
'Tag','Text1', ...
'VerticalAlignment','cap', ...
'Visible','off');
set(get(c,'Parent'),'XLabel',c);
c = text('Parent',b, ...
'Color',[0 0 0], ...
'HandleVisibility','callback', ...
'HorizontalAlignment','center', ...
'Position',[-0.0767918 0.502137 0], ...
'Rotation',90, ...
'Tag','Text2', ...
'VerticalAlignment','baseline', ...
'Visible','off');
set(get(c,'Parent'),'YLabel',c);
c = text('Parent',b, ...
'Color',[0 0 0], ...
'HandleVisibility','callback', ...
'HorizontalAlignment','right', ...
'Position',[-0.151877 1.08333 0], ...
'Tag','Text3', ...
'Visible','off');
set(get(c,'Parent'),'ZLabel',c);
c = text('Parent',b, ...
'Color',[0 0 0], ...
'HandleVisibility','callback', ...
'HorizontalAlignment','center', ...
'Position',[0.5 1.0235 0], ...
'Tag','Text4', ...
'VerticalAlignment','bottom', ...
'Visible','off');
set(get(c,'Parent'),'Title',c);
% bin2eight.m
function y = bin2eight(x)
% bin2eight
%
% Converts binary data to an eight bit form
% Accepts 1x8 array and returns the corresponding decimal
y = 0;
k = 0;
for i = 1:8
y = y + x(8-k)*2^k;
k = k+1;
end
% bin2pol.m
function y = bin2pol(x)
% bin2pol
% Converts binary numbers (0,1) to polar numbers (-1,1)
% Accepts a 1-D array of binary numbers
y = ones(1,length(x));
for i = 1:length(x)
if x(i) == 0
y(i) = -1;
end
end
30
% ch.m
% ch
recv = xmit; % channel is applied to recv, don't modify transmitted data
if channel_on == 1
disp('Simulating Channel')
norm_factor = max(abs(recv)); % Normalize all data before applying
recv = (1/norm_factor) * recv; % channel for a fair comparison
ch_clipping
ch_multipath
ch_noise
recv = norm_factor * recv; % Restore data magnitude for proper decoding
end
% ch_clipping.m
% ch_clipping
for i = 1:length(recv)
if recv(i) > clip_level
recv(i) = clip_level;
end
if recv(i) < -clip_level
recv(i) = -clip_level;
end
end
% ch_multipath.m
% ch_multipath
copy1=zeros(size(recv));
for i=1+d1:length(recv)
copy1(i)=a1*recv(i-d1);
end
copy2=zeros(size(recv));
for i=1+d2:length(recv)
copy2(i)=a2*recv(i-d2);
end
recv=recv+copy1+copy2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -