Matlab Assignment (COMPLETE)
Matlab Assignment (COMPLETE)
SEM 1 2015/2016
NAME
SCHOOL
: MECHANICAL ENGINEERING
LECTURER
Voltage, V(V)
Time, t(ms)
Sampled Data for Sampling Frequency of 2000 Hz
Matlab Code
%Purpose: i)To
using fft function
%
ii)To
components and
No.
1
2
3
4
5
6
7
8
9
10
11
12
13
No.
14
15
16
17
18
19
20
21
22
23
24
25
Time(ms)
(ms)
Time
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
5.5
6
Voltage(V)
5.5
7
-2
-10
-6
7.1
6
2.5
-2
-4.5
-4.5
-1.5
4
6.5
7
7.5
8
8.5
9
9.5
10
10.5
11
11.5
12
7.4
3
-8
-10
2
7.5
4.5
0.5
-3
-4.8
-2.5
3
(B)
Sampling
Frequency of 500 Hz
Voltage, V(V)
Time, t(ms)
Time (ms)
0
2
4
6
8
10
12
Voltage(V)
5.5
-5
-2
4
-10
0.5
3
Matlab Code
%Purpose: i)To obtain the frequency spectrum using fft function in Matlab
%
ii)To determine the frequency components and their corresponding amplitudes
close all; %clear all the data
fid = fopen('EMC_500Hz.txt', 'r'); %open the file from the text
A = fscanf(fid, '%g %g', [2 inf]); % read the data from the 2 columns of the text file
B=A'; % transfer the data from the text file
t=B(:,1); % 1 refer to the data at column 1
v=B(:,2); %2 refer to the data at column 2
plot(t,v);
xlabel('Time(ms)');
ylabel('Voltage(V)');
title('Graph of Voltage(V) versus Time(ms)at Sampling Frequency 500Hz');
f_sampling=500; %sampling frequency is equal to 500Hz
y=fft(v); %voltage undergoes fast fourier transform
N=length(y); %N is the numbers of sampling data
amplitude=abs(y)*2/N; %calculate the actual amplitude
f=(1:N)/N; %normalize the scale
frequency=f*f_sampling; %calculate the actual frequency
figure, plot(frequency,amplitude) %plot a graph of actual amplitude versus actual frequency
xlabel('Frequency(Hz)');
ylabel('Amplitude');
title('Frequency Spectrum for Sampling Frequency 500Hz');
Figure 3 :
Frequency Spectrum
Figure 4:
Voltage, V(V)
Time, t(ms)
Time (ms)
0
1
2
3
4
5
6
7
8
9
10
11
12
Voltage(V)
5.5
-2
-5
6
-2
-4.5
4
3
-10
7.5
0.5
-4.8
3
Matlab Code
%Purpose: i)To obtain the frequency spectrum using fft function in Matlab
%
ii)To determine the frequency components and their corresponding amplitudes
close all; %clear all the data
fid = fopen('EMC_1000Hz.txt', 'r'); %open the file from the text
A = fscanf(fid, '%g %g', [2 inf]); % read the data from the 2 columns of the text file
B=A'; % transfer the data from the text file
t=B(:,1); % 1 refer to the data at column 1
v=B(:,2); %2 refer to the data at column 2
plot(t,v);
xlabel('Time(ms)');
ylabel('Voltage(V)');
title('Graph of Voltage(V) versus Time(ms)at Sampling Frequency 1000Hz');
f_sampling=1000; %sampling frequency is equal to 1000Hz
y=fft(v); %voltage undergoes fast fourier transform
N=length(y); %N is the numbers of sampling data
amplitude=abs(y)*2/N; %calculate the actual amplitude
f=(1:N)/N; %normalize the scale
frequency=f*f_sampling; %calculate the actual frequency
figure, plot(frequency,amplitude) %plot a graph of actual amplitude versus actual frequency
xlabel('Frequency(Hz)');
ylabel('Amplitude');
title('Frequency Spectrum for Sampling Frequency 1000Hz');
Figure 5: Graph
Spectrum
Figure
6: Frequency
Voltage, V(V)
Time, t(ms)
Sampled Data for Sampling Frequency of 4000Hz
Voltage(V
No. Time Time
(ms) (ms) Voltage(V)
1
0.00
5.50)
2
0.25
7.00
3
0.50
7.00
4
0.75
5.00
5
1.00
-1.00
6
1.25
-7.00
7
1.50
-10.50
8
1.75
-10.50
9
2.00
-5.00
10
2.25
2.00
11
2.50
6.00
12
2.75
7.20
13
3.00
6.00
14
3.25
4.50
15
3.50
2.50
16
3.75
0.00
17
4.00
-2.00
18
4.25
-3.50
19
4.50
-4.50
20
4.75
-5.00
21
5.00
-4.50
22
5.25
-3.50
23
5.50
-1.50
24
5.75
0.50
25
6.00
4.00
No.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
6.25
6.50
6.75
7.00
7.25
7.50
7.75
8.00
8.25
8.50
8.75
9.00
9.25
9.50
9.75
10.00
10.25
10.50
10.75
11.00
11.25
11.50
11.75
12.00
6.00
7.20
6.50
3.00
-2.00
-8.00
-10.00
-10.00
-3.00
4.00
7.00
7.20
6.50
5.00
3.00
0.50
-1.50
-3.00
-4.30
-4.80
-4.00
-2.50
-0.50
2.50
Matlab Code
%Purpose: i) To obtain the frequency spectrum using fft function in Matlab
%
ii) To determine the frequency components and their corresponding amplitudes
close all; %clear all the data
fid = fopen('EMC_4000Hz.txt', 'r'); %open the file from the text
A = fscanf(fid, '%g %g', [2 inf]); % read the data from the 2 columns of the text file
B=A'; % transfer the data from the text file
t=B(:,1); % 1 refer to the data at column 1
v=B(:,2); %2 refer to the data at column 2
plot(t,v);
xlabel('Time(ms)');
ylabel('Voltage(V)');
title('Graph of Voltage(V) versus Time(ms)at Sampling Frequency 4000Hz');
f_sampling=4000; %sampling frequency is equal to 4000Hz
y=fft(v); %voltage undergoes fast fourier transform
N=length(y); %N is the numbers of sampling data
Figure 7:Graph
Spectrum
Figure
8: Frequency
Frequency
(Hz)
80
Amplitude(V)
160
0.2954
240
0.11
320
0.05577
400
6.937
480
Frequency(Hz)
560
71.43
640
142.9
214.3 720
800
Frequency of 2000 Hz
0.096
Frequency of 500 Hz
0.1358
Amplitude
(V)
2.863
1.143
0.3391
2.951
1.207
5.147
0.1137
880
0.2598
960
0.3495
1040
0.4646
285.7
4.106
Frequency(Hz)
81.63
163.26
244.89
326.52
408.15
489.78
571.41
Frequency(Hz)
653.04
76.92734.67
153.8 816.3
897.93
230.8979.56
307.71061.19
384.61142.82
1224.45
461.51306.08
538.51387.71
1469.34
1550.97
1632.6
1714.23
1795.86
1877.49
1959.12
2040.75
Amplitude(V)
0.1429
0.3559
0.4042
0.3004
6.8
0.4382
2.994
Amplitude(V)
0.2237
1.084
0.1846
0.1719
0.65
0.4266
0.6799
0.1114
0.208
0.6554
6.370.07
0.1841
2.086
0.03028
2.233
0.1573
0.09163
0.07481
0.1103
0.06876
0.1076
0.1083
0.1018
0.05263
Amplitude, (V)
Actual
Measured
Amplitude
Amplitude
6.7
6.937
2.9
2.863
0.9
1.207
Frequency, f (Hz)
Amplitude, (V)
Percentage of
Difference (%)
3.54
1.28
34.11
Measured
Amplitude
5.147
4.106
-
Percentage of
Difference
23.18 (%)
41.59
-
Amplitude, (V)
Actual
Measured
Amplitude
Amplitude
6.7
6.37
2.9
2.086
0.9
2.233
Percentage of
Difference (%)
4.93
28.07
148.11
Amplitude, (V)
Actual
Measured
Amplitude
Amplitude
6.7
6.8
2.9
2.994
0.9
1.084
Percentage of
Difference (%)
1.49
3.24
20.44
st
1
2nd
3rd
Actual
Frequency
322.6
483.9
645.2
Measured
Frequency
214.3
285.7
-
Percentage of
Difference
33.57
40.96
-
Actual
Amplitude
6.7
2.9
0.9
DISCUSSIONS:
The answer given in (page 774) is V(t) = 6.7cos (2026.8t) + 2.9sin (3040.3) - 0.9
cos(4053.7t). Since w = 2f, the answer can also be written as V(t) = 6.7cos (2322.6t) + 2.9sin
(2483.9t) - 0.9cos (2645.2t). From this equation, the amplitudes are 6.7V, 2.9V and 0.9V with
signal frequencies = 322.6Hz, 483.9Hz and 645.2Hz respectively.
For sampling frequency of 2000 Hz, the Nyquist frequency is 1000Hz. 3 of the signal
frequencies(322.6Hz,483.9Hz,645.2Hz) are smaller than the Nyquist frequency(1000Hz)
.Therefore, no aliasing occurs and the percentage of differences for the frequency and amplitudes
obtained are less than 35%.
For sampling frequency of 500 Hz, the Nyquist frequency is 250 Hz. 3 of the signal
frequencies (322.6Hz, 483.9Hz, and 645.2Hz) are greater than the Nyquist frequency which is
250 Hz. Hence, aliasing occurs for the 3 amplitude obtained. Besides, the third harmonic
frequency cannot be detected because the sampled frequency deviates greatly from the Nyquist
frequency.
For sampling frequency of 1000Hz, the Nyquist frequency is 500 Hz. The highest signal
frequency (645.2Hz) is greater than the Nyquist frequency whereas the others 2 signal
frequencies arent. Therefore, the first and second amplitude obtained contain small percentage
error of 4.93% and 28.07% whereas the third signal has large percentage error of 148.11%.
Therefore, aliasing occurs for the 645.2Hz signal frequency.
For sampling frequency of 4000 Hz, the Nyquist frequency is 2000Hz. 3 of the signal
frequencies detected are less than this Nyquist frequency. Hence, the percentage error for the
frequency and the amplitude obtained are small which range from 13% to 27% and 1% to 25%
respectively. Therefore, no aliasing occur.
Nyquist frequency state that signals with frequency lower than fNyq = fs/2 are accurately
sampled. Signals with frequencies greater than or equal to fNyq are not accurately sampled and
appears as lower frequencies in the discrete sample.
If the frequency used is lower than the Nyquist rate. It will exhibit aliasing. Aliasing is the
presence of unwanted components in the reconstructed signal and it occurs whenever the Nyquist
frequency falls below the signal frequency.
Therefore to get more accurate result, it is necessarily to sample at twice the maximum
frequency of the signal. The sampling theorem state that a signal can be exactly reproduces if it
is sample at a frequency, F where F is greater than twice the maximum frequency in the signal.
By comparing figure 1, 3, 5 and 7 with the trace from the oscilloscope, it is found that
sampling frequency of 2000Hz and 4000 Hz can plot a quite similar graph to the original trace
from the oscilloscope whereas sampling frequencies of 500Hz and 1000 Hz acts otherwise. This
is because aliasing occur in both 500Hz and 1000 Hz sampling frequencies.
CONCLUSION:
From the above calculation using Matlab, it is concluded that aliasing will occur if the
highest signal frequency is greater than the Nyquist frequency.
For this trace from an oscilloscope (Voltage against Time), the sampling frequency
should be more than 2X 645.2Hz (highest signal frequency) to avoid aliasing. The sampling
frequencies of 2000Hz and 4000 Hz are appropriate whereas sampling frequencies of 500Hz and
1000 Hz is not suitable as aliasing occurs.