03-Signal and Data
03-Signal and Data
1.1 Introduction
Suppose you have a signal generator so you have the capability of generating a square
wave with 1 Hz frequency. You adjust the generator, so the low level of the signal is
0 v. and the high level is 1 v. Figure 1.1 shows 3 s of such signal:
The signal in Fig. 1.1 repeats three consecutive times the same pattern. It is a
periodic signal with period T = 1 s.
© Springer Science+Business Media Singapore 2017 3
J.M. Giron-Sierra, Digital Signal Processing with Matlab Examples, Volume 1,
Signals and Communication Technology, DOI 10.1007/978-981-10-2534-1_1
4 1 Periodic Signals
0.5
-0.5
0 0.5 1 1.5 2 2.5 3
seconds
Suppose you also have a computer with a data acquisition channel, so it is possible
to get samples of the 1 Hz square signal. For instance, let us take 10 samples per
second. In this case, you get from 3 s of signal, a data set like the following:
A = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0];
From this data set, with 30 numbers, it is possible to reproduce the signal, provided
information about time between samples is given. In order to have such information,
another parallel set of 30 sampling times could be recorded when you get the data;
or just keep in memory or paper what the sampling frequency was (or the total time
of signal that was sampled).
Figure 1.2 plots the data set A versus 30 equally spaced time intervals along the
3 s.
The MATLAB code to generate Fig. 1.2 is the following:
Program 1.1 Square signal
% Square signal
A=[1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,...
1,1,1,1,1,0,0,0,0,0];
fs=10; %sampling frequency in Hz
tiv=1/fs; %time interval between samples;
t=0:tiv:(3-tiv); %time intervals set (30 values)
plot(t,A,'*'); %plots figure
axis([0 3 -0.5 1.5]);
xlabel('sec.'); title('square wave samples');
MATLAB handles signals using data sets (vectors). Usually the Signal Processing
Toolbox routines ask for the signal samples data set, and for information about the
sampling frequency.
1.3 Generation of Periodic Signals 5
0.5
-0.5
0 0.5 1 1.5 2 2.5 3
seconds
Let us use the periodic signals included in the Signal Processing Toolbox.
1.3.1 Sinusoidal
Figure 1.3 shows the results of the Program 1.2, it is a 180 points data set covering
3 s of the 1 Hz signal.
In the next program, both sine and cosine signals, with the same 1 Hz frequency,
are generated.
6 1 Periodic Signals
0.5
-0.5
-1
-1.5
0 0.5 1 1.5 2 2.5 3
seconds
0.5
-0.5
-1
-1.5
0 0.5 1 1.5 2 2.5 3
seconds
Figure 1.4 shows the results of Program 1.3, two superimposed (using hold on)
signals. Notice the 90◦ phase difference between both signals.
As shall be seen shortly, periodic signals can be decomposed into a sum of sine
and cosine signals.
1.3.2 Square
The following MATLAB program generates 0.03 s of a square signal with 0.01 s
period (100 Hz frequency). The signal generation is based on the use of the square()
function. A feature of this function is that it is possible to specify the duty cycle (the
percent of the period in which the signal is positive), in order to generate rectangular
signals.
Program 1.4 Square signal
% Square signal
fy=100; %signal frequency in Hz
wy=2*pi*fy; %signal frequency in rad/s
duy=0.03; %signal duration in seconds
fs=20000; %sampling frequency in Hz
tiv=1/fs; %time interval between samples;
t=0:tiv:(duy-tiv); %time intervals set
y=square(wy*t); %signal data set
plot(t,y,'k'); %plots figure
axis([0 duy -1.5 1.5]);
xlabel('seconds'); title('square signal');
0.5
-0.5
-1
-1.5
0 0.005 0.01 0.015 0.02 0.025 0.03
seconds
8 1 Periodic Signals
1.3.3 Sawtooth
The following MATLAB program generates 0.03 s of a sawtooth signal with 0.01 s
period (100 Hz frequency). The signal generation is based on the use of the sawtooth()
function.
Program 1.5 Sawtooth signal
% Sawtooth signal
fy=100; %signal frequency in Hz
wy=2*pi*fy; %signal frequency in rad/s
duy=0.03; %signal duration in seconds
fs=20000; %sampling frequency in Hz
tiv=1/fs; %time interval between samples;
t=0:tiv:(duy-tiv); %time intervals set
y=sawtooth(wy*t); %signal data set
plot(t,y,'k'); %plots figure
axis([0 duy -1.5 1.5]);
xlabel('seconds'); title('sawtooth signal');
0.5
-0.5
-1
-1.5
0 0.005 0.01 0.015 0.02 0.025 0.03
seconds
1.3 Generation of Periodic Signals 9
1.5 1.5
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-1.5 -1.5
0 0.01 0.02 0.03 0 0.01 0.02 0.03
seconds seconds
1.5 1.5
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-1.5 -1.5
0 0.01 0.02 0.03 0 0.01 0.02 0.03
seconds seconds
1.5
0.5
-0.5
-1
-1.5
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
seconds
We can add, subtract, multiply, etc. signals. It is interesting to deal, in this section,
with some basic examples.
In Program 1.8 we add three sinusoidal signals. This is done in the sentence with the
following expression:
Notice that the amplitude and frequency of the three sinusoids are the following:
Figure 1.9 shows the result of adding the three sinusoidal signal. It is a periodic
signal with frequency 300 Hz. It looks similar to a square signal.
Fourier series are sums of sinusoidal harmonics. In the case of Program 1.8 we
are taking the first three non-zero harmonics of the series corresponding to a square
12 1 Periodic Signals
1.5
0.5
-0.5
-1
-1.5
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
seconds
1.5.2 Multiplication
Figure 1.10 shows the result of multiplying two sinusoidal signals. One of the signals
has 70 Hz frequency and the other has 2 Hz frequency. Both signals have amplitude 1.
The result obtained is a modulated signal. Chapter 8 of this book is devoted to
modulation, and a similar example will be seen in more detail.
Program 1.9 has been used to generate the Fig. 1.10. It includes a sentence with
a sound() function. When hearing the modulated signal, notice the tremolo effect
caused by the 2 Hz signal. The Internet address of a tutorial on sound amplitude
modulation has been included in the Resources section.
1.5 Operations with Signals 13
0.5
-0.5
-1
-1.5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
seconds
Periodic signals can be decomposed into sums of sine and cosine signals, according
with the following expression, which is a Fourier series:
∞
∞
y(t) = a0 + an cos (n · w0 t) + bn sin(n · w0 t) (1.2)
n=1 n=1
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 0.5 1 1.5 2 2.5 3
seconds
The sine signal is an odd signal. All other odd signals can be obtained with:
∞
y(t) = bn sin (n · w0 t) (1.4)
n=1
Let us consider the example shown in Fig. 1.11. It is a sawtooth signal (which is an
odd signal).
Figure 1.11 has been obtained with the Program 1.10 (very similar to
Program 1.5)
Program 1.10 Sawtooth signal to be analyzed
%sawtooth signal to be analyzed
fy=1; %signal frequency in Hz
wy=2*pi*fy; %signal frequency in rad/s
Ty=1/fy; %signal period in seconds
N=256;
fs=N*fy; %sampling frequency in Hz
1.6 Harmonics. Fourier 15
By using the function fft() (fast Fourier transform) we can obtain the values of
coefficients bi in Eq. (1.4). Let us apply this function for the y(t) sawtooth signal.
Program 1.11 obtains the first ten coefficients. Notice that it is enough to pass to fft()
one signal period.
Program 1.11 Fourier transform of sawtooth signal
%Fourier Transform of sawtooth signal
fy=1; %signal frequency in Hz
wy=2*pi*fy; %signal frequency in rad/s
Ty=1/fy; %signal period in seconds
N=256;
fs=N*fy; %sampling frequency in Hz
tiv=1/fs; %time interval between samples;
t=0:tiv:(Ty-tiv); %time intervals set
y=sawtooth(wy*t); %signal data set
fou=fft(y,fs); %Fourier Transform (set of complex numbers)
hmag=imag(fou); bh=hmag/N; %get set of harmonic amplitudes
stem(0:9,bh(1:10)); %plot of first 10 harmonics
axis([0 10 0 1]);
xlabel('Hz'); title('sawtooth signal harmonics');
Figure 1.12 shows the values of the coefficients bi versus frequencies 0, ω0 , 2ω0 ,
3ω0 , etc. in Hz.
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 1 2 3 4 5 6 7 8 9 10
Hz
16 1 Periodic Signals
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.5 1 1.5 2 2.5 3
seconds
The cosine signal is an even signal. All other even signals can be obtained with:
∞
y(t) = a0 + an cos (n · w0 t) (1.6)
n=1
Now, let us apply fft() to obtain the an coefficients in Eq. (1.6). This is done with
the Program 1.13.
Program 1.13 Fourier transform of rectified signal
%Fourier Transfom of rectified sine signal
fy=1; %signal frequency in Hz
wy=2*pi*fy; %signal frequency in rad/s
Ty=1/fy; %signal period in seconds
N=256;
fs=N*fy; %sampling frequency in Hz
tiv=1/fs; %time interval between samples;
t=0:tiv:(Ty-tiv); %time intervals set
y=abs(sin(wy*t)); %signal data set
fou=fft(y,fs); %Fourier Transform (set of complex numbers)
hmag=real(fou); ah=hmag/N; %get set of harmonic amplitudes
stem(0:9,ah(1:10)); hold on; %plot of first 10 harmonics
plot([0 10],[0 0],'k');
xlabel('Hz'); title('rectified sine signal harmonics');
Figure 1.14 shows the values of the coefficients ai versus frequencies 0, ω0 , 2ω0 ,
3ω0 , etc. in Hz.
Since the rectified sine has a non-zero average value, Fig. 1.14 shows a non-zero
value of a0 .
0.4
0.2
-0.2
-0.4
-0.6
0 1 2 3 4 5 6 7 8 9 10
Hz
18 1 Periodic Signals
For this kind of signals, the harmonics for frequencies 0, 2ω0 , 4ω0 , 8ω0 , etc. have
zero amplitude.
Consider the signal shown in Fig. 1.15. It is a triangular signal, which has half-
wave symmetry. It is also an even signal (it will have only cosine harmonics).
The signal in Fig. 1.15 has been obtained with the Program 1.14.
Program 1.14 Triangular signal to be analyzed
%triangular signal to be analyzed
fy=1; %signal frequency in Hz
wy=2*pi*fy; %signal frequency in rad/s
Ty=1/fy; %signal period in seconds
N=256;
fs=N*fy; %sampling frequency in Hz
tiv=1/fs; %time interval between samples;
t=0:tiv:((3*Ty)-tiv); %time intervals set (3 periods)
y3=-sawtooth(wy*t,0.5); %signal data set
plot(t,y3'k');
xlabel('seconds'); title('triangular signal (3 periods)');
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 0.5 1 1.5 2 2.5 3
seconds
1.6 Harmonics. Fourier 19
0.6
0.5
0.4
0.3
0.2
0.1
-0.1
0 1 2 3 4 5 6 7 8 9 10
Hz
Figure 1.16 shows the values of the coefficients ai versus frequencies 0, ω0 , 2ω0 ,
3ω0 , etc. in Hz.
Notice that amplitudes of harmonics with frequencies 0, 2ω0 , 4ω0 , 6ω0 , etc. are
zero.
0.8
0.6
0.4
0.2
The width of the high level parts of the signal can be modified by changing the
value of W in Program 1.16.
Again, let us apply fft() to obtain the an coefficients corresponding to the pulse
train signal, which is an even signal (Program 1.17).
Program 1.17 Fourier transform of pulse train signal
%Fourier Transform of pulse train signal
fy=1; %signal frequency in Hz
wy=2*pi*fy; %signal frequency in rad/s
Ty=1/fy; %signal period in seconds
N=256; W=20;
fs=N*fy; %sampling frequency in Hz
y1=zeros(256,1); y1(1:W)=1; y1((256-W):256)=1; %signal period
fou=fft(y1,fs); %Fourier Transform (set of complex numbers)
hmag=real(fou); ah=hmag/N; %get set of harmonic amplitudes
1.6 Harmonics. Fourier 21
Figure 1.18 shows the values of the coefficients ai versus frequencies 0, ω0 , 2ω0 ,
3ω0 , etc. in Hz.
For a certain value nc, it will happen that all coefficients an with n>nc will have
an absolute value less than a0 /100. Let us take this value nc as a practical limit of
the number of harmonics that deserve to be considered. Also, let us take wnc as the
maximum frequency of interest for the study of the signal.
If you decrease the value of W in Program 1.17, making the pulses to narrow, you
will notice an increase of nc and of wnc . From the point of view of data transmission
it means that the narrower the pulses to be transmitted through a channel (for instance
a wire), the larger the channel bandwidth must be.
Look at the data points in Fig. 1.18. They draw a peculiar curve that we shall meet
again in other parts of the book. It corresponds to the sinc() function:
sin(x)
sinc(x) = (1.8)
x
Figure 1.19 depicts part of the sinc(t) function. It is usual to represent this function
with respect to negative and positive values of time.
0.25
0.2
0.15
0.1
0.05
-0.05
-0.1
0 5 10 15 20 25 30 35 40 45 50
Hz
22 1 Periodic Signals
0.8
0.6
0.4
0.2
-0.2
-0.4
-6 -4 -2 0 2 4 6
seconds
Notice than all signals in Sect. 1.3. have been generated with a sampling frequency
that is higher enough with respect to the signal frequency. Let us see what happens
lowering the sampling frequency. Next program generates a 1 Hz sinusoidal signal
using a 7 Hz sampling frequency.
1.7 Sampling Frequency 23
Figure 1.20 shows the results of Program 1.19. Notice how the signal looks dis-
torted with respect to a pure sine.
It is interesting to play with the Program 1.19, changing the sampling frequency
and looking at the results. In particular, there may appear peculiar effects if the
sampling frequency is equal or lower than two times the signal frequency.
For example let us consider the case of the sampling frequency and the signal
frequency being equal. In this case the signal data points draw a horizontal line. It
seems that there is a simple constant (DC) signal. This can happen, and has happened,
in reality: you measure at a certain sampling frequency, you believe from sampled
data that there is a constant signal, but it is not true (what you see on the computer
screen is an impostor, a signal “alias”) [2, 5].
So it is important to determine the frequency of the signal you are sampling.
0.5
-0.5
-1
-1.5
0 0.5 1 1.5 2 2.5 3
seconds
24 1 Periodic Signals
1.5
1
0.5
fs=100 0
-0.5
-1
-1.5
0 0.5 1 1.5 2 2.5 3
1.5
1
0.5
fs=4
0
-0.5
-1
-1.5
0 0.5 1 1.5 2 2.5 3
seconds
Figure 1.21 shows on top the 3 Hz sinusoidal signal. The plot below shows the
data obtained sampling the sinusoidal signal: by joining the data points it seems that
the data obtained correspond to a triangular signal. Again this is a mistake, we see
on screen an “alias” of the real sinusoidal signal.
In the case of sinusoidal signals, the sampling frequency should be higher than
two times the signal frequency [1, 7]. If this is the case of a non-sinusoidal periodic
signal, the highest frequency of interest wnc for this signal must be determined (see
the case of the pulse train in Sect. 1.6.4.) and the sampling frequency should be
higher than two times this frequency (Shannon’s theorem) [4].
This is a simple exercise that may lead to interesting observations. The idea is to
create a signal by concatenating a series of sinc functions, and then apply fft( ) to this
signal.
(e) Analysis of a modulated signal
The modulated signals obtained by multiplication have interesting harmonics con-
tents. It would be opportune to apply fft( ) and discuss the result.
One experiment could be to take a sound file and try to modulate it with a sinusoid,
and see what happens.
1.9 Resources
1.9.1 MATLAB
1.9.1.2 Toolboxes
References
1. P. Cheung, Sampling & Discrete Signals. Lecture presentation, Imperial College Lon-
don (2011). http://www.ee.ic.ac.uk/pcheung/teaching/ee2_signals/Lecture2013-Sampling&
discrete-signals.pdf
2. M. Handley, Audio Basics. Lecture presentation, University College London (2002). www0.cs.
ucl.ac.uk/teaching/Z24/02-audio.pdf
3. J.H. McClellan, R.W. Schafer, M.A. Yoder, Signal Processing First (Prentice Hall, Upper Saddle
River, 2003)
4. T.K. Moon, Sampling. Lecture Notes, UtahState Univ. (2006). http://ocw.usu.edu/Electrical_
and_Computer_Engineering/Signals_and_Systems/lecture6.pdf
5. B.A. Olshausen. Aliasing. Lecture Notes, UC Berkeley (2000).
redwood.berkeley.edu/bruno/npb261/aliasing.pdf
6. H.D. Pfister. Fourier Series Synthesizer. Lecture Notes, Duke Univ. (2013). http://pfister.ee.
duke.edu/courses/ecen314/project1.pdf
7. J. Schesser. Sampling and Aliasing. Lecture presentation, New Jersey Institute of Technology
(2009). https://web.njit.edu/~joelsd/Fundamentals/coursework/BME310computingcw6.pdf