EE1/EIE1: Introduction To Signals and Communications MATLAB Experiments
EE1/EIE1: Introduction To Signals and Communications MATLAB Experiments
Use the following MATLAB code to use the fft function to perform Fourier
transform on the generated rectangular pulse signal and visualize the
magnitude of the rectangular pulse signal in frequency domain.
y=fftshift(fft(x)); % apply Fourier transform and move zero
frequency component to the center
N=length(y); % measure frequency range
1
n=-(N-1)/2:(N-1)/2; % evenly divide frequency range around zero
frequency
f=sqrt(y.*conj(y)); % calculate amplitude of the frequency signal
plot(n,f); % visualize the signal in time domain
title('Frequency spectrum amplitude for the rectangular pulse');
% set title of the plot
xlabel('Frequency (Hz)'); % label x-axis
ylabel('Frequency spectrum amplitude'); % label y-axis
axis([-50 50 0 70000]); % set display range of x- and y-axis
Experiment 1.3 – Convert from frequency to time domain with ideal low-pass
filter (i.e., loss of high-frequency signal components)
Instead of using all frequency-domain components to reconstruct the time-
domain signal, we only select and use a range of low-frequency components to
reconstruct the time-domain signal.
In order to achieve this, we construct a filter that only lets low-frequency signal
components to pass through, while blocking high frequency components. Use
the following MATLAB code to realize the low-pass filter.
w = 50; % tunable parameter for filter bandwidth
fil=sign(n+w)-sign(n-w); % specify the low-pass filter
plot(n,fil); % visualize the filter response
title('Ideal low-pass filter'); % set title of the plot
xlabel('Frequency (Hz)'); % label x-axis
ylabel('Amplitude'); % label y-axis
axis([-(w+20) (w+20) 0 3]); % set display range of x- and y-axis
2
It can be seen that the filter is a rectangular pulse signal around zero frequency
in frequency domain. Then we apply this filter to the frequency-domain signal
and perform the inverse Fourier transform on the filtered frequency-domain
signal.
w = 50; % tunable filter bandwidth
fil=sign(n+w)-sign(n-w); % specify the low-pass filter
f2=fil.*y; % f2 is the filtered output of original frequency signal
Y1=ifft(f2)/2; % apply inverse Fourier transform to the filtered signal
plot (t,abs(Y1)); % reconstructed filtered signal in time domain
hold on; % hold for another plot
plot (t,abs(Y)); % reconstructed unfiltered signal in time domain
legend('Reconstructed signal with loss (filtered)', 'Reconstructed signal
without loss'); % legend of two plots
title('Compare signal reconstructed from filtered Fourier series with the
original signal');% set title of the plot
xlabel('Time (msec)'); % label x-axis
ylabel('Original or reconstructed'); % label y-axis
axis([-(10+T) (10+T) 0 3]); % set display range of x- and y-axis
3
Snare drum http://audio.routledge-
interactive.s3.amazonaws.com/9780240821535/mixing_home/gated_kick.mp
3
Please click the above URL links to download the audio files. Make sure that
these audio files are downloaded into your current MATLAB directory. First,
use the following MATLAB code to load and visualize the signal in time domain.
s = audioread('LRMonoPhase4.wav');
s = s(:,1); % extract one sound track only
% load different audio source by changing the file name in ('')
plot(s); % visualize the signal in time domain
Next, perform Fourier transform and visualize the audio signal in frequency
domain. Use the following MATLAB code.
Fs = 44100; % sample rate of the audio signal
N = length(s); % the number of samples of the audio signal
transform = fft(s,N); % apply Fourier transform
magTransform = abs(transform); % magnitude of the FFT
faxis = ((-0.5:1/N:0.5-1/N)*Fs).'; % frequency range of the signal
plot(faxis,fftshift(magTransform));
xlabel('Frequency (Hz)');
ylabel('Spectrum magnitude');
xlim([-1000 1000]);
Using the following code, you can plot all three signals in a single plot to
compare the differences of their frequency-domain components among the
audio signals.
s1 = audioread('LRMonoPhase4.wav');
s2 = audioread('piano2.wav');
s3 = audioread('gated_kick.mp3');
Fs = 44100;
N1 = length(s1);
N2 = length(s2);
N3 = length(s3);
transform1 = fft(s1,N1);
transform2 = fft(s2,N2);
transform3 = fft(s3,N3);
magTransform1 = abs(transform1);
magTransform2 = abs(transform2);
magTransform3 = abs(transform3);
faxis1 = ((-0.5:1/N1:0.5-1/N1)*Fs).';
faxis2 = ((-0.5:1/N2:0.5-1/N2)*Fs).';
faxis3 = ((-0.5:1/N3:0.5-1/N3)*Fs).';
plot(faxis1,fftshift(magTransform1(:,1)),'m-+');
hold on
plot(faxis2,fftshift(magTransform2(:,1)),'b-s');
plot(faxis3,fftshift(magTransform3),'k-p');
xlim([-2000 2000]);
lgd = legend('male human voice','piano','snare drum');
lgd.FontSize = 10;
set(gca,'fontsize',10)
grid on
xlabel('Frequency (Hz)');
4
Experiment 2.2 - Fourier transforms of your own voice signal
In this experiment, you have the opportunity to record and analyse your own
voice signal. Please make sure that the microphone is turned on and connected
to your computer. The audio recording is done via the audiorecorder
function in MATLAB. This function is used to create the audiorecorder
object, which may take the following parameters:
After creating the audiorecorder object, you can start recording your
voice by using a microphone connected to the mic input of a computer. To
start, pause, resume and terminate the recording, use the following commands,
respectively.
record(arec)
pause(arec)
resume(arec)
stop(arec)
5
Then, use the following command to extract the recorded data.
data = getaudiodata(arec);
Finally, use the recorded voice signal as input and repeat the steps in
Experiment 2.1, use the following code.
Fs = 44100; % sample rate of the audio signal
N = length(data); % the number of audio signal samples
transform = fft(data,N); % apply Fourier transform
magTransform = abs(transform); % magnitude of the FFT
faxis = ((-0.5:1/N:0.5-1/N)*Fs).'; % frequency range of the signal
plot(faxis,fftshift(magTransform));
xlabel('Frequency (Hz)');
ylabel('Spectrum magnitude');
Experiment 3: Modulation
You will experiment signal modulation in MATLAB. Two modulation methods,
namely Amplitude Modulation (AM) and Frequency Modulation (FM), will be
explored here. In general, any type of modulation method typically involves
three main steps: (1) generate the modulating signal, (2) generate the carrier
signal, and (3) modulate the carrier signal with the modulating signal. These
steps will first be demonstrated separately in the AM experiment below.
Experiment 3.1 - Amplitude modulation (AM)
First, generate and visualize the modulating signal using the following MATLAB
code.
Am=2; % set the amplitude of modulating signal
fa=2000; % set the frequency of modulating signal
Ta=1/fa; % set the time period of modulating signal
t=0:Ta/999:6*Ta; % set the total time for simulation
ym=Am*sin(2*pi*fa*t); % synthesize the modulating signal
plot(t,ym); % visualize the modulating signal
grid on; % include grid in the plot
title ('Modulating signal');
xlabel ('Time');
ylabel ('Modulating signal m(t)');
ylim([-5 5]);
Next, generate and visualize the carrier signal using the following MATLAB
code.
Ac=3; % set the amplitude of carrier signal
fc=fa*10; % set the frequency of carrier signal
6
Tc=1/fc; % set the time of carrier signal
yc=Ac*sin(2*pi*fc*t); % synthesize the carrier signal
plot(t,yc); % visualize the carrier signal
grid on; % include grid in the plot
title ('Carrier signal');
xlabel ('Time (sec)');
ylabel ('Carrier signal fc(t)');
ylim([-5 5]);
Finally, modulate the carrier signal with the modulating signal, and visualize
the modulated signal using the following MATLAB code.
A=3; % AM carrier offset
y=(A+Am*sin(2*pi*fa*t)).*sin(2*pi*fc*t); % Full AM with carrier offset A
plot(t,y);
grid on;
title ('Amplitude modulated signal');
xlabel ('Time (sec)');
ylabel ('Signal');
axis([0 2*10^(-3) -7 7]);
In order to compare the modulating signal, the carrier signal, and the
modulated signal in time domain, you can plot them on top of each other in
the same figure using MATLAB function subplot.
subplot(3,1,1);plot(t,ym);
ylim([-6 6]);
ylabel('Signal');xlabel('Time');title('Modulating signal');
subplot(3,1,2);plot(t,yc);
ylim([-6 6]);
ylabel('Signal');xlabel('Time');title('Carrier signal');
subplot(3,1,3);plot(t,y);
ylim([-6 6]);
ylabel('Signal');xlabel('Time');title('Full AM modulated signal');
7
Experiment 3.2 - Frequency modulation (FM)
To experiment with FM in MATLAB, the three steps given above are applicable.
However, instead of using pre-determined parameters, this time you will be
given the opportunity to set experiment parameters, including the carrier
frequency and the modulating signal frequency, by pop-up prompt in the
command window. Use the following MATLAB code to achieve this.
clear;clc
fm=input('Message Frequency = (recommended value: 25)');
fc=input('Carrier Frequency = (recommended value: 400)');
mi=input('Modulation Index = (recommended value: 10)');
t=0:0.0001:0.11;
m=sin(2*pi*fm*t);
c=sin(2*pi*fc*t);
y=sin(2*pi*fc*t+(mi.*-cos(2*pi*fm*t)));
% note that the integral of sin(x) is -cos(x);
figure;
subplot(3,1,1);plot(t,m); ylim([-2 2]);
ylabel('Signal');xlabel('Time');title('Modulating signal');
subplot(3,1,2);plot(t,c); ylim([-2 2]);
ylabel('Signal');xlabel('Time');title('Carrier signal');
subplot(3,1,3);plot(t,y); ylim([-2 2]);
ylabel('Amplitude');xlabel('Time');title('FM signal');
Fs = 10000;
y1=fftshift(fft(m,length(m)));
N1=length(y1);
n1=(-0.5:1/N1:0.5-1/N1)*Fs;
f1=sqrt(y1.*conj(y1));
y2=fftshift(fft(c,length(c)));
N2=length(y2);
n2=(-0.5:1/N2:0.5-1/N2)*Fs;
f2=sqrt(y2.*conj(y2));
y3=fftshift(fft(y,length(y)));
N3=length(y3);
n3=(-0.5:1/N3:0.5-1/N3)*Fs;
f3=sqrt(y3.*conj(y3));
figure;
subplot(3,1,1);plot(n1,f1); ylim([0 600]); xlim([-1000 1000]);
ylabel('Amplitude');xlabel('Frequency');title('Modulating signal');
subplot(3,1,2);plot(n2,f2); ylim([0 600]); xlim([-1000 1000]);
ylabel('Amplitude');xlabel('Frequency');title('Carrier signal');
subplot(3,1,3);plot(n3,f3); ylim([0 600]); xlim([-1000 1000]);
ylabel('Amplitude');xlabel('Frequency');title('FM signal');
Similar to Experiment 3.1, the above code generates one figure showing the
modulating signal, the carrier signal and the frequency modulated signal, as
well as a second figure showing their frequency-domain equivalence. You can
re-run this experiment with the modulating and carrier signals of different
frequencies.
8
Discrete Fourier Transform (DFT)
0 g(t)e
T
G( ) j t
dt
N 1
lim g(nTs )e j nTsTS where N T / TS
Ts 0
n0 1
N 1
Gm g n e
jmn0TS
where g n g(nTS )TS
n0