245 lines
7.7 KiB
Matlab
245 lines
7.7 KiB
Matlab
function txspectrum(scenario, bw, fs, fshift, L)
|
|
if nargin == 0
|
|
fprintf('Use this function to plot your Transmit spectrum\n');
|
|
fprintf('\n');
|
|
fprintf('First argument is to run 5 example scenarios. \n');
|
|
fprintf('txpsectrum(1) or txspectrum(2)...to plot and study various scenarios\n');
|
|
fprintf('\n');
|
|
fprintf('For custom numbers function can be called as follows\n');
|
|
fprintf('txspectrum([], data bandwidth, sampling frequency,shift or carrier frequency, x-axis plotting limit(optional arugment)\n');
|
|
elseif (nargin == 1)
|
|
%% System Parameters
|
|
sys = [];
|
|
sys.scenario = scenario;
|
|
switch sys.scenario
|
|
case 1
|
|
% Use Nyquist Zone 1 to tx signal at center frequency = 1 GHz with Fs = 4
|
|
% GHz.
|
|
sys.fs = 4e9;
|
|
sys.bw = 400e6;
|
|
sys.fshift = 1e9;
|
|
L = 7;
|
|
sys.xlim = [-L L];
|
|
sys.xticks = -L : L;
|
|
case 2
|
|
% Use Nyquist Zone 2 to tx signal at center frequency = 3 GHz with Fs = 4
|
|
% GHz.
|
|
sys.fs = 4e9;
|
|
sys.bw = 400e6;
|
|
sys.fshift = -1e9;
|
|
L = 7;
|
|
sys.xlim = [-L L];
|
|
sys.xticks = -L : L;
|
|
case 3
|
|
% This is a difficult scenario. We want to tx the signal at center frequency
|
|
% = 1.8 GHz. But with Fs = 4 GHz, there is also a copy of the spectrum
|
|
% centered at 2.2 GHz touching the spectrum at 1.8 GHz. Unless we have a
|
|
% ideal brick-wall analog lowpass filter, we cannot remove the spectrum at
|
|
% 2.2 GHz.
|
|
%
|
|
sys.fs = 4e9;
|
|
sys.bw = 400e6;
|
|
sys.fshift = 1.8e9;
|
|
L = 7;
|
|
sys.xlim = [-L L];
|
|
sys.xticks = sort([-L : -3, -1 : 1, 3:L, 1.8, -1.8, 2.2, -2.2]);
|
|
case 4
|
|
% This scenario is a solution to scenario 3. There are two key ideas:
|
|
%
|
|
% (1) When converting complex baseband signal to real bandpass signal, use
|
|
% a value of fshift that is large enough to avoid overlap between the
|
|
% spectra at +ve frequency and -ve frequency. In our example, the
|
|
% complex baseband signal has frequency content from -200 MHz to 200
|
|
% MHz, so we can pick fshift = -400 MHz and the spectrum at -400 MHz
|
|
% does not overlap with the spectrum at 400 MHz.
|
|
%
|
|
% (2) Digital signal's spectrum repeats every Fs. Use this fact to create
|
|
% spectrum at higher frequency. We have the desired spectrum at -0.4
|
|
% GHz and so use Fs = 2.2 GHz to create the spectrum at -0.4 GHz + 2.2
|
|
% GHz = 1.8 GHz.
|
|
%
|
|
% In this scenario, we use a lower sampling rate of 2.2 GHz and use Nyquist
|
|
% zone 2 to transmit our signal at 1.8 GHz. Now there is a resonable gap
|
|
% between the spectrum at 1.8 GHz and the spectrum at 2.6 GHz and we can
|
|
% rely on a bandpass filter to filter everything except the the spectrum at
|
|
% 1.8 GHz.
|
|
%
|
|
sys.fs = 2.2e9;
|
|
sys.bw = 400e6;
|
|
sys.fshift = -0.4e9;
|
|
L = 5;
|
|
sys.xlim = [-L L];
|
|
sys.xticks = sort([-L : L, 1.8, -1.8, 2.2, -2.2, 0.4, -0.4, 2.6, -2.6]);
|
|
case 5
|
|
% Better solution than scenario 4.
|
|
sys.fs = 2.6e9;
|
|
sys.bw = 400e6;
|
|
sys.fshift = -0.8e9;
|
|
L = 5;
|
|
sys.xlim = [-L L];
|
|
sys.xticks = sort([-L : L, 1.8, -1.8, 2.2, -2.2, 0.4, -0.4, 2.6, -2.6]);
|
|
otherwise
|
|
error('Invalid sys.scenario');
|
|
end
|
|
elseif (nargin == 4) %if(nargin == 1)
|
|
|
|
%% System Parameters
|
|
sys = [];
|
|
sys.scenario = scenario;
|
|
sys.bw = bw;
|
|
sys.fs = fs;
|
|
sys.fshift = fshift;
|
|
L = 7;
|
|
sys.xlim = [-L L];
|
|
sys.xticks = -L : L;
|
|
elseif (nargin == 5)
|
|
%% System Parameters
|
|
sys = [];
|
|
sys.scenario = scenario;
|
|
sys.bw = bw;
|
|
sys.fs = fs;
|
|
sys.fshift = fshift;
|
|
sys.xlim = [-L L];
|
|
sys.xticks = -L : L;
|
|
end
|
|
sys.xunit = 'GHz';
|
|
|
|
|
|
%%
|
|
if nargin > 0
|
|
if isempty(findobj('Number', sys.scenario))
|
|
figure(sys.scenario)
|
|
set(gcf, 'Position', [227 140 1231 617])
|
|
else
|
|
figure(sys.scenario)
|
|
clf
|
|
end
|
|
|
|
|
|
%%
|
|
subplot(311)
|
|
xlim(sys.xlim);
|
|
xticks(sys.xticks);
|
|
set(gca, 'Position', [0.0463 0.7521 0.9277 0.18])
|
|
f1 = 0 + (-4:4)*sys.fs;
|
|
plotspectrum('asym', f1, sys.bw, 'r', sys.xunit);
|
|
s = sprintf('Complex Baseband (Fs = %.2f GHz. BW = %d MHz)', ...
|
|
sys.fs/1e9, sys.bw/1e6);
|
|
plotannotations(sys.fs, s)
|
|
addtext3(-4.5, 1.15, '|Z(f)|')
|
|
|
|
|
|
%%
|
|
subplot(312)
|
|
xlim(sys.xlim);
|
|
xticks(sys.xticks);
|
|
set(gca, 'Position', [0.0463 0.4221 0.9277 0.18])
|
|
f1 = f1 + sys.fshift;
|
|
plotspectrum('asym', f1, sys.bw, 'r', sys.xunit);
|
|
s = sprintf('Complex Bandpass (Fs = %.2f GHz. ', sys.fs/1e9);
|
|
if abs(sys.fshift) >= 1e9
|
|
s = [s, sprintf('fshift = %.2f GHz)', sys.fshift/1e9)];
|
|
else
|
|
s = [s, sprintf('fshift = %d MHz)', sys.fshift/1e6)];
|
|
end
|
|
plotannotations(sys.fs, s)
|
|
addtext3(-4.5, 1.15, '|U(f)|')
|
|
|
|
|
|
%%
|
|
subplot(313)
|
|
xlim(sys.xlim);
|
|
xticks(sys.xticks);
|
|
set(gca, 'Position', [0.0463 0.0921 0.9277 0.18])
|
|
f2 = -f1;
|
|
plotspectrum('asym', f1, sys.bw, 'r', sys.xunit);
|
|
plotspectrum('asymflip', f2, sys.bw, 'r', sys.xunit);
|
|
s = sprintf('Real Bandpass (Fs = %.2f GHz)', sys.fs/1e9);
|
|
plotannotations(sys.fs, s)
|
|
addtext3(-4.5, 1.15, '|S(f)| = |Y(f)|')
|
|
|
|
|
|
end
|
|
end
|
|
|
|
|
|
% ------------------------------------------------------------------------------
|
|
% Helper Functions
|
|
% ------------------------------------------------------------------------------
|
|
|
|
|
|
function plotannotations(fs, titlestr)
|
|
|
|
%% Plot DC.
|
|
xline(0, 'LineWidth', 3, 'Color', 'g', 'LineStyle', '--')
|
|
y = 0.9;
|
|
addtext1(0, y, '0')
|
|
|
|
%% Plot fs/2.
|
|
xline(fs/2/1e9, 'LineWidth', 3, 'Color', 'g', 'LineStyle', '--')
|
|
xline(-fs/2/1e9, 'LineWidth', 3, 'Color', 'g', 'LineStyle', '--')
|
|
addtext1(0 + fs/2/1e9, y, 'fs/2')
|
|
addtext1(0 - fs/2/1e9, y, '-fs/2')
|
|
|
|
%% Plot fs.
|
|
xline(fs/1e9, 'LineWidth', 3, 'Color', 'g', 'LineStyle', '--')
|
|
xline(-fs/1e9, 'LineWidth', 3, 'Color', 'g', 'LineStyle', '--')
|
|
addtext1(0 + fs/1e9, y, 'fs')
|
|
addtext1(0 - fs/1e9, y, '-fs')
|
|
|
|
%% Plot 3*fs/2.
|
|
xline(fs*1.5/1e9, 'LineWidth', 3, 'Color', 'g', 'LineStyle', '--')
|
|
xline(-fs*1.5/1e9, 'LineWidth', 3, 'Color', 'g', 'LineStyle', '--')
|
|
addtext1(0 + 1.5*fs/1e9, y, '3*fs/2')
|
|
addtext1(0 - 1.5*fs/1e9, y, '-3*fs/2')
|
|
|
|
%% Plot ...
|
|
ntext4(0 + 1.6*fs/1e9, 0.6, '...', 'FontSize', 30, 'FontWeight', 'bold');
|
|
ntext4(0 - 1.7*fs/1e9, 0.6, '...', 'FontSize', 30, 'FontWeight', 'bold');
|
|
|
|
%%
|
|
addtext2(fs/4/1e9, 0.7, {'Nyquist', 'Zone 1'})
|
|
addtext2(fs/4/1e9 + fs/2/1e9, 0.7, {'Nyquist', 'Zone 2'})
|
|
% addtext2(fs/4/1e9 + fs/1e9, 0.7, {'Nyquist', 'Zone 3'})
|
|
|
|
%% Title.
|
|
title(titlestr, 'FontSize', 12);
|
|
|
|
%%
|
|
h = gca;
|
|
h.XAxis.FontSize = 11;
|
|
|
|
end
|
|
|
|
|
|
function addtext1(x, y, t)
|
|
|
|
h = ntext4(x, y, t);
|
|
h.FontSize = 15;
|
|
h.FontWeight = 'bold';
|
|
h.HorizontalAlignment = 'center';
|
|
|
|
end
|
|
|
|
|
|
function addtext2(x, y, t)
|
|
|
|
h = ntext4(x, y, t);
|
|
h.FontSize = 15;
|
|
h.FontWeight = 'bold';
|
|
h.Color = [0 0.4470 0.7410];
|
|
h.HorizontalAlignment = 'center';
|
|
|
|
end
|
|
|
|
|
|
function addtext3(x, y, t)
|
|
|
|
h = ntext4(x, y, t);
|
|
h.FontSize = 20;
|
|
h.FontWeight = 'bold';
|
|
h.Color = 'r';
|
|
h.HorizontalAlignment = 'center';
|
|
|
|
end
|