DSP Lab 0
DSP Lab 0
Lesson 2
Signal Basics
2.1 Introduction
After you have finished studying the first lesson, you will have read that Matlab is the most
powerful technical programming tool in signal processing and communications, but you don't yet
know why. The reason is that Matlab has the power of dealing directly with large arrays in the
same degree of ease of dealing with single numbers. This gives Matlab a very high preference to
people studying signal processing and communications. Signals are usually large arrays of
numbers. People want to operate on those signals as fast and simple as possible. Thus, Matlab is
the best choice. This automatically makes Matlab the top choice for people studying
communication, as communication consists completely of signal processing operations.
In this chapter, we will study the basics of using Matlab in signal processing. We will focus in
this chapter on four fundamental issues: creating elementary signals, performing elementary
operations on those signals, plotting signals, and computing signal statistics. Thus, we have
divided this chapter into four sections. Section I deals with creating elementary signals such as
sine waves, square waves, impulses … etc. Section II deals with performing elementary signal
operations such as arithmetic operations, upsampling and downsampling, rectification … etc.
Section III deals with plotting and signal visualization. Section IV deals with signal statistics and
how they may be computed using Matlab.
(a) (b)
Figure 2.1
Looking carefully at figure 2.1.a, you can see that the time interval between each sample and
the next is 0.1. In figure 2.1.b, however, this value is 0.05. Thus, we have seen how changing the
sample time will change the shape of the signal.
It seems usually that working with smaller sample times makes the representation of the signals
more accurate. We will discuss this issue in the next point. However, for now we will say that this is
Figure 2.2
When we try to create any periodic signal, we will choose the sample time such that there are at
least 10 samples per period. In our words, the sample rate will be at least 10 times the frequency
of the signal.
Example:
For a triangular wave of frequency 250 Hz, the minimum sample rate will be 2500 Hz. This
corresponds to a sample time of 400 s. The sample time must be less than or equal to 400 s.
DC Signal
A DC signal may be easily generated using the "zeros" or "ones" functions. The syntax of both
functions is the same. This syntax is shown below.
Ramp Signal
A ramp signal is a signal whose value increases or decreases uniformly with time. A ramp
signal has the following general form:
y (t ) = at + b
The parameter a is called the slope, and the parameter b is called the intercept of the signal.
Example
In the following example, we are generating a ramp signal of sample rate 1000 Hz, slope -2
volts/second and intercept -3 volts in the time range from -2 seconds to 4 seconds.
>>t=linspace(-2,4,6*1000);
>>x=-2*t-3;
Polynomial Signals
Ramp signals are a special case of polynomial signals. In the following example, we will see
how a polynomial signal of higher degree may be generated.
Example
The polynomial signal we want to generate a polynomial signal that follows the equation:
x (t ) = 3t 3 − 11t 2 + 7
We want to generate this signal in the time range from 0 to 1 second with a sample rate of 1000
Hz. The following instructions generate this signal.
>>t=linspace(0,1,1000);
>>x=3*t.^3-11*t.^2+7;
Polynomial signals require a time base signal to be generated, because they are computed as
functions in time.
>>c=0.3*a;
This instruction multiplies the signal a by a constant gain. We may use the (*) operator or the
(.*) operator when one of the operand is a scalar.
>>c=c=a/0.4;
This instruction divides the signal a by the scalar 0.4. We may use the (./) or (/) operand when
the divisor is a scalar..
>>c=a^2;
This instruction raises the elements in a to a fixed power. We may use the (^) or (.^) operand
when the power is a scalar.
Clipping
In the following example, a sine wave of amplitude 5 is generated. This wave will then be
clipped at +3 volts and -4 volts. This means that any input value greater than +3 should be equal to
+3 at the output, and any input value less than -4 should be equal to -4 at the output. The following
instructions generate the sine wave and then subject it to the required clipping operation.
>>t=linspace(0,4,400);
>>x=5*sin(2*pi*t);
>>I1=find(x>3);
>>x(I1)=3;
>>I1=find(x<-4);
>>x(I1)=-4;
The third instruction finds the elements whose magnitudes are greater than +3, and outputs
their location indices in I1. The fourth instruction writes +3 in these locations in x. The fifth
instruction finds the elements whose magnitudes are less than -4, and outputs their location
indices in I2. The sixth instruction writes -4 in these locations in x. Obviously, the new x is clipped
at +3 and -4.
Signal Switching
Signal switching is a common operation in which we have multiple input signals and one output
signal. The output signal may follow either of the input signals depending on a certain condition.
For example, if the condition is satisfied, the output equals the first signal. If not, it follows the
second signal. The following example demonstrates a signal switching operation with three input
signals and one output signal. The output continuously follows the maximum of the three inputs.
>>t=linspace(0,4,400);
>>x1=4*sin(2*pi*t);
>>x2=3*sin(2*pi*t+2*pi/3);
>>x3=3*cos(2*pi*t);
>>x1greatest=(x1>x2)&(x1>x3);
>>x2greatest=(x2>x1)&(x2>x3);
>>x3greatest=(x3>x1)&(x3>x2);
>>y=x1greatest.*x1+ x2greatest.*x2+ x3greatest.*x3;
Differentiation
Determining the derivative of a signal is a crucial part in many systems. The following example
explains how the first derivative of a signal may be obtained. In a repetitive manner, we may obtain
higher derivatives. The differencing process is carried out using the "diff" function. However, we
must multiply by the sample rate (or divide by the sample time) because the derivative should
equal the sample differences over the time differences.
>>t=linspace(0,4,400);
>>x=5*sin(2*pi*t).*exp(-0.5*t);
>>x_dash=100*diff(x);
Autocorrelation
Autocorrelation is a very important operation. It is a correlation of a signal with itself.
Autocorrelation is useful in detecting the fundamental period of a noisy signal of unknown
frequency, in determining the power spectral density of a signal, and much more. Thus,
autocorrelation appear very frequently in speech or voice analysis, spectral analysis … etc. Given
a signal of dimensions 1×N, the autocorrelation output will be a signal of dimensions 1×2N-1. The
following example shows how the autocorrelation of a signal may be computed.
>>x=[0 0 1 1 1 1 0 0];
>>xACR=xcorr(x);
Figure 2.5
Figure 2.6
Figure 2.8
Figure 2.9
Figure 2.11
The following instructions were written to generate this figure. The two signals in the top two
boxes are called s1 and s2. The two signals in the wide axes box are called s and g.
>>t1=linspace(0,1e-6,20);
>>s1=[0:0.1:0.9 ones(1,10)];
>>s2=-s1(end:-1:1);
2.11.3 Histograms
A signal histogram is a histogram that illustrates the distribution of the values of signal samples.
We may generate the histogram of any signal x by typing the following instruction:
>>hist(x,N)
N is the number of bins in the histogram. Figures 2.12 and 2.13 show two histograms of a
sinusoidal signal generated using 30 bins in the first case and 100 bins in the second case.
Figure 2.13
1- Compute the mean, variance, and average power of the signal A2Q1.
2- Write down the Matlab instructions that will generate the signal depicted below. This signal
consists of a DC segment from -2 to 0 seconds, a quarter cycle of a sinusoidal wave from 0 to 1
seconds, and another DC segment from 1 to 3 seconds. The sample rate is 100 Hz.
3- The two signals A2Q3_1 and A2Q3_2 have a sample rate of 1000 Hz, and are defined over the
interval from 0 to 4 seconds. Produce a signal that is equal to the first signal from 0 to 1 second,
the second signal from 1 to 2 seconds, the first signal from 2 to 3 seconds, and the second signal
from 3 to 4 seconds.
4- A team of engineers is trying to choose a pair of pulses to use in transmitting binary information
in a digital communication system. There are two pairs of pulses. The first pair is given in the
signals A2Q4_1_0 and A2Q4_1_1. A2Q4_1_0 is used to transmit logic 0, whereas A2Q4_1_1 is
used to transmit logic 1. The second pair is given in the signals A2Q4_2_0 and A2Q4_2_1.
A2Q4_2_0 is used to transmit logic 0, whereas A2Q4_2_1 is used to transmit logic 1. On the
average, 72% of the bits transmitted will be zeros. If the engineers will choose the pulse pair that
reduces the average transmission power, which pair should they choose?
5- The noise signal given in A2Q5 has an unknown distribution. Generate the histogram of this
signal, and state whether this signal appears to have a uniform or normal distribution. If the signal
has a uniform distribution, computes its upper and lower limits. If the signal has a normal
distribution, compute its mean and standard deviation.