1
7.18 Write a MATLAB user-defined function that displays a plot of the normalized power (energy) spec-
trum as a function of the frequency. Name the function EnergySpec(t,f). The input arguments t and f
are vectors with the values of the independent and dependent variables of a function that is given by a
finite number of data points, respectively. The function first uses MATLAB’s built-in function fft to cal-
culate the DFT and then calculates and plots the normalized power (energy) spectrum as a function of the
frequency.
Consider f ( t ) from Problem 7.17. Write a MATLAB program is a script file that samples f ( t ) using
the user-defined function Sampling from Problem 7.17. Then, use EnergySpec to make the normalized
power (energy) spectrum plots. Execute the program for the following four cases:
–5
(a) Interval of 0.01s, sampling interval of 1 × 10 s.
–5
(b) Interval of 0.1s, sampling interval of 1 × 10 s.
–5
(c) Interval of 1s, sampling interval of 1 × 10 s.
–5
(d) Interval of 2s, sampling interval of 1 × 10 s.
The output plots should resemble closely the plot in Fig. 7-4.
Solution
The user-defined function EnergySpec:
function EnergySpec(t,f)
fs=1/(t(2)-t(1))
N=length(f);
Nh=fix(N/2);
F=fft(f)/N;
Fh=F(1:Nh);
power=Fh.*conj(Fh)/N;
powerNor=power/max(power);
fk=(1:N/2)*(fs/N);
plot(fk,powerNor,'k')
xlabel('Frequency (Hz)')
ylabel('Normalized')
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2
(a) Script file:
clear, clc
v=554.365;
pv=pi*v;
ti=0.005;
foft= @ (t) 0.5*sin(2*pv*t)*exp(-1E5*(t-ti)^2)+sin(4*pv*t)*exp(-1E5*(t-
ti)^2)...
+0.2*sin(6*pv*t)*exp(-1E4*(t-ti)^2)+0.1*sin(8*pv*t)*exp(-1E4*(t-ti)^2)...
+0.1*sin(10*pv*t)*exp(-1E5*(t-ti)^2);
[t,f]=Sampling(foft,0.01,1E-5);
plot(t,f)
xlabel('t (s)')
ylabel('f(t)')
figure
EnergySpec(t,f)
axis([0 5000 0 1.2])
Figures displayed:
1.5
1.2
1
1
0.5
0.8
Normalized
f(t)
0
0.6
-0.5
0.4
-1
0.2
-1.5
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01 0
0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
t (s)
Frequency (Hz)
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3
(b) Script file:
clear, clc
v=554.365;
pv=pi*v;
ti=0.005;
foft= @ (t) 0.5*sin(2*pv*t)*exp(-1E5*(t-ti)^2)+sin(4*pv*t)*exp(-1E5*(t-
ti)^2)...
+0.2*sin(6*pv*t)*exp(-1E4*(t-ti)^2)+0.1*sin(8*pv*t)*exp(-1E4*(t-ti)^2)...
+0.1*sin(10*pv*t)*exp(-1E5*(t-ti)^2);
[t,f]=Sampling(foft,0.1,1E-5);
plot(t,f)
axis([0 0.05 -1.5 1.5])
xlabel('t (s)')
ylabel('f(t)')
figure
EnergySpec(t,f)
axis([0 5000 0 1.2])
Figures displayed:
1.5 1.2
1 1
0.5 0.8
Normalized
f(t)
0 0.6
-0.5 0.4
-1 0.2
-1.5 0
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
t (s) Frequency (Hz)
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
4
(c) Script file:
clear, clc
v=554.365;
pv=pi*v;
ti=0.005;
foft= @ (t) 0.5*sin(2*pv*t)*exp(-1E5*(t-ti)^2)+sin(4*pv*t)*exp(-1E5*(t-
ti)^2)...
+0.2*sin(6*pv*t)*exp(-1E4*(t-ti)^2)+0.1*sin(8*pv*t)*exp(-1E4*(t-ti)^2)...
+0.1*sin(10*pv*t)*exp(-1E5*(t-ti)^2);
[t,f]=Sampling(foft,1,1E-5);
plot(t,f)
axis([0 0.05 -1.5 1.5])
xlabel('t (s)')
ylabel('f(t)')
figure
EnergySpec(t,f)
axis([0 5000 0 1.2])
Figures displayed:
1.5
1.2
1
1
0.5
0.8
Normalized
f(t)
0
0.6
-0.5
0.4
-1
0.2
-1.5
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 0
t (s) 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
Frequency (Hz)
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
5
(d) Script file:
clear, clc
v=554.365;
pv=pi*v;
ti=0.005;
foft= @ (t) 0.5*sin(2*pv*t)*exp(-1E5*(t-ti)^2)+sin(4*pv*t)*exp(-1E5*(t-
ti)^2)...
+0.2*sin(6*pv*t)*exp(-1E4*(t-ti)^2)+0.1*sin(8*pv*t)*exp(-1E4*(t-ti)^2)...
+0.1*sin(10*pv*t)*exp(-1E5*(t-ti)^2);
[t,f]=Sampling(foft,2,1E-5);
plot(t,f)
axis([0 0.05 -1.5 1.5])
xlabel('t (s)')
ylabel('f(t)')
figure
EnergySpec(t,f)
axis([0 5000 0 1.2])
Figures displayed:
1.5 1.2
1 1
0.5 0.8
Normalized
f(t)
0 0.6
-0.5 0.4
-1 0.2
-1.5 0
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000
t (s) Frequency (Hz)
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.