Matlab Ex1
Matlab Ex1
300
COMMUNICATION THEORY
Matlab Exercise #1
1 SAMPLING
The modeled signals and systems in this course are mostly analog (continuous‐time). However, the
computer based simulation environment is always digital (discrete‐time), of course.
Thus, in Matlab, we have certain constraints from the DSP (Digital Signal Processing) world, such as
A continuous time signal xc(t) is represented by a vector, [xc(t0) xc(t1) ... xc(tN-1)], whose
length is N, i.e. the signal is truncated and sampled
Time interval tN‒1 ‒ t0 and sampling interval Ts=ti+1 ‒ ti depend on the signal
Time interval is chosen large enough to obtain enough information
Sampling frequency is typically chosen much higher than the Nyquist frequency, 2·fmax
o Sampling frequency Fs=1/Ts is chosen enough large so that the sampled signal “looks
like” an analog signal in Matlab. The rule of thumb is at least 10 times the maximum
frequency component
EXAMPLE: RUN THE FOLLOWING STATEMENTS (IN THE COMMAND WINDOW OR IN A SCRIPT FILE)
TO CREATE A 50 HZ SINUSOIDAL SIGNAL:
X ( j ) xc (t )e jt dt Continuous time FT, CTFT
j k
Xˆ (e j ) x(k )e Discrete time FT, DTFT
k
N 1 j 2kn / N
X (n) x(k )e , n 0,1,..., N 1 Discrete FT, DFT
k 0
Above and below x(k) is the sampled signal from the xc(t)
x(k)=xc(kTs)
xc(t)
X(f)
(N-1)Ts f
Ts kTs t B B
^ X(n)
X(f)
0 1 N/2 N-1
FFT calculation is not related to the actual sampling frequency Fs, but it is given as 0 … ((N-1)/N)·2π
where π refers to half the sample rate. For plotting you have to generate the vector of frequency
points. Frequency resolution F0 depends on sampling frequency and the length of the signal vector.
With real signals all the information is between 0 – Fs/2, but during this course we prefer to plot the
spectrum in the two‐sided format (‐Fs/2…Fs/2).
EXAMPLE: RUN THE FOLLOWING STATEMENTS (IN THE COMMAND WINDOW OR IN A SCRIPT FILE)
TO PLOT THE DFT OF THE 50 HZ SINUSOIDAL SIGNAL:
>>F_x=fft(x); % DFT of X, saved to Fx
>>Nx=length(x);
>>Fo=1/(Ts*Nx); % frequency resolution.
>>freq1=0:Fo:(Nx-1)*Fo; % One-sided frequency Axis
>>figure
>>plot(freq1,abs(F_x)/Nx) % One-sided amplitude Spectrum
>>freq2=-Nx/2*Fo:Fo:(Nx/2-1)*Fo; % Two-sided frequency Axis
>>figure
>>plot(freq2,fftshift(abs(F_x)/Nx)) % Two-sided amplitude Spectrum
Scaling by Nx is required in Matlab in order to get correct amplitude values in the frequency domain.
However, often it is more important to see the frequency content in general (and especially relative
amplitudes between separate frequencies), in which case the scaling is not as essential. Fftshift is
used to generate the two‐sided spectrum plot (‐Fs/2…Fs/2).
Time domain
Frequency domain
3 SPECTRAL ILLUSTRATIONS
At this point, if not yet done, you should create a new Matlab script‐file (.m) and write there the
rest of the Matlab commands in this exercise (do the same also for the following Matlab
exercises). Below you’ll find a few tips, which might be useful when working with the scripts:
It can be useful to start the script as “clear; close all; clc”, which automatically clears
the old variables from the desktop (clear), closes all the open figures (close all), and clears
the command window from the text (clc), when starting to run the script.
Note that in the script you can “comment” any line or part of a line by adding the %‐sign
(note that the shortcut to comment the whole line at once is ctrl+r). After the %‐sign, the
following statements in the same line are ignored when the script is executed.
You can execute/run a small part of the script by highlighting text, and then pressing F9.
Now only the highlighted statements are executed.
Remember also the Run‐button in the editor window to run the whole script at once (this
will also save the script)
You can create a code section by adding two %‐signs as %%. This might help in creating a
more logical structure for the script (for example dividing the script into sections based on
the exercise task numbers). You can run one section by selecting the section and pressing
ctrl+Enter, or from the editor window by “Run section”.
Notice that the time and frequency plotting commands can be mostly copy‐pasted for the
corresponding plots in the following tasks!
3.2 MULTIPLICATION BETWEEN TWO SIGNALS
Let’s consider a new signal as s(t) = x(t)·m(t), where m(t) is a sine wave with frequency of 750 MHz.
4 LINEAR FILTERING
Filter the signal y(t) (from task 3.3) with the generated Butterworth filter (help filter)
o y_filtered_butter = filter(…);
Plot the filtered signal in time and frequency domain
o Notice the filter delay
Compare the original y(t) and the filtered y(t) with each other in time domain and frequency
domain
o What “part” of the signal is still visible after filtering in time/frequency domain?
4.2 BAND PASS FIR (FINITE IMPULSE RESPONSE) FILTER
Create a band pass FIR filter (help firpm, see the figure below)
o Order of 60
o Bass‐band: 1.3 GHz…1.8 GHz
o Transition bands: 0.8 GHz…1.3 GHz and 1.8 GHz…2.3 GHz
Illustration of arbitrary chosen f and a vectors used in the bass band filter design with firpm
(b = firpm(n,f,a), where n is the filter order, and vectors f and a are illustrated below)
Plot the impulse response of the filter by using stem‐function (help stem)
stem(-order/2:order/2,b)
Filter the signal y(t) (from task 3.3) with the generated FIR filter (help filter)
y_filtered_FIR = filter(b,1,y);