signals
signals
(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
true. Working with smaller sample times, however, may lead to a storage problem. To see how,
suppose that we want to define a signal of sample time 1 ns over a time duration of 1 second. If
each sample is defined using 8 bytes, the total storage size required to store this signal will be
approximately 8 Gigabytes! Keep in mind that signals are stored in the RAM. Such signal would be
very difficult to create, and will probably make your machine crash.
Thus, it is very important to determine the sample rate of the signal carefully. Determining the
proper sample rate is a search for the best value on the following line.
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.
trigonometric signals, exponential signals, logarithmic signals, square signals. We will then explain
the Matlab functions responsible for the direct generation of all the elementary signals. Afterwards,
we will discuss the techniques of indirect generation.
Figure 2.3
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.
x1 = zeros 1, N
x 2 = ones 1, N
The "zeros" function generates a 1×N vector of zeros. The "ones" function generates a 1×N
vector of ones. Using basic operations, we could generates DC signals of any other value, as the
following examples demonstrate.
Examples
The first example below shows an instruction used to generate a DC signal of value -5 and
sample rate 100 Hz in the time interval from -3 to 3 seconds using the "zeros" function. The second
example shows an instruction used to generate a DC signal of value 17.3 and sample rate 20 Hz in
the time interval from 0 to 4 seconds using the "ones" function. Note that generating a DC signal
does not need a time base signal, because the signal is not a function in time.
>>x=zeros(1,600)-5;
>>x=17.3*ones(1,80);
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.
Exponential functions
exp
Logarithmic functions
log, log10
Examples
In the following examples, we will generate the following six signals. All signals have an interval
of interest from 1 to 5 seconds, and a sample rate of 150 Hz.
y 1 t = 3 cos 4 t + / 4
y 2 t = 4 sinh 3.5t + 2
y 3 t = 4e −0.5t
y 4 t = 2 − 0 .6 t + 0 .3
y 5 t = ln 3 / t
y 6 t = log 0.6t
>>t=linspace(1,5,600);
>>y1=3*cos(4*pi*t+pi/4);
>>y2=4*sinh(3.5*t+2);
>>y3=4*exp(-0.5*t);
>>y4=2.^(-0.6*t+0.3);
>>y5=log(3./t);
>>y6=log10(0.6*t);
>>c=a-b;
This instruction subtracts the signal b from the signal a. Again, a and b must have the same
dimensions.
>>c=a.*b;
This instruction multiplies the two signals together. The two signals must have the same
dimensions. Note that the multiplication operator is (.*) not (*).
>>c=c=a./b;
This instruction divides the signal a by the signal b. The two signals must have the same
dimensions. Note that the division operator is (./) not (/).
>>c=a.^b
This instruction raises the elements in a to the powers of elements in b.
>>c=a+3;
This instruction adds a DC offset to signal a. This is an exception to the size rule we mentioned
before.
>>c=a-3;
This instruction subtracts a DC offset from signal a. This is an exception to the size rule we
mentioned before.
>>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.
The fourth instruction downsamples x1 by a factor of 1.5. The result is a 1×8 vector with the
values: [1.94 3.88 …]. These numbers are obtained by reconstructing the signal and then sampling
the reconstructed signal at the destination sample rate.
interest that is twice the original may be obtained by upsampling the signal by a factor of 2. We
may upsample the signal using the "upsample" function or the "resample" function. If we are
considering the signal to be discrete, using "upsample" will be better, because discrete signals are
upsampled by zero insertion between the original samples. If we are considering the signal
continuous, using "resample" will be better, because continuous signals are upsampled by
interpolating between the original samples. In our example, we are considering all our signals
continuous. Thus, we have used "resample" to upsample the signal up to the number of points of
the new interval of interest.
The seventh and eighth instructions are used to generate the fifth required signal. Since this
signal is compressed in time by a factor of 4, the interval of interest of the new compressed signal
will be from -0.5 to 1. This new interval of interest requires us to define a new time base signal. The
number of points of this time base signal will be 1500, because the sample rate equals 1000 Hz.
Thus, the new time base signal is obtained using linspace(-0.5,1,1500). Now, the compressed
signal is obtained using the "downsample" function because the downsampling factor of 4 is an
integer. If the downsampling factor was not an integer, the "downsample" function would not have
been able to perform, and thus we would have used "resample" in this case.
The remaining instructions are used to open figures and plot the signals. We will deal with these
instructions in the next section of this lesson.
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.
In the fourth instruction, the search for elements in x1 that are less than zero is written in the
place of, and thus represents, the indices in x1 in which 0 will be written. In the last instruction, x is
multiplied by the result of the relational test x>0. This test will return 1 at the locations of positive
signal samples and 0 otherwise. Multiplying these two signals together is equivalent to "zeroing"
the negative portions of x.
Full Wave Rectification
Full wave rectification is the process of negating the negative signal portions, such that the
signal becomes all-positive. Full wave rectification may be simply done using the "abs" function,
which removes any negative signs in the signal. The following example demonstrates this.
>>t=linspace(0,4,400);
>>x=3*sin(2*pi*t);
>>x1=abs(x);
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);
Cross Correlation
Cross correlation is the correlation between two different signals. Measuring the correlation
between two different signals is a very important operation in matched filters and optimum receiver
design, object detection, signal analysis … etc. The following example shows how the cross
correlation between two signals may be computed.
>>x1=[0 0 1 1 1 1 0 0];
>>x2=[0 0 1 1 1 0 1 0]
>>CCSignal=xcorr(x1,x2);
Figure 2.4
Figure 2.5
Figure 2.6
Figure 2.7
Figure 2.8
Figure 2.9
Figure 2.10
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);
Signal Processing Lab
29
Alexandria University – SSP sixth term of computer and communications (DSP)
>>t6=linspace(0,6e-6,120);
>>s=[s1 s1 s2 s1 s2 s2];
>>P=o.5*ones(1,20);
>>g=[P P –P P –P -P];
>>subplot(2,2,1);
>>plot(t1,s1);
>>subplot(2,2,2);
>>plot(t1,s2);
>>subplot(2,2,[3 4]);
>>plot(t6,g);
>>hold on;
>>plot(t6,s);
The first subplot command divides the figure into 2×2 partitions, and creates and activates the
first axes box, which is situated in the top left partition of the figure. Any subsequent plotting
command will target this axes box. The second subplot command creates and activates the
second axes box, which is situated in the top right partition of the figure. Thus, the subsequent plot
goes to this axes box. The third subplot command creates and activates an axes box that spans
the third and fourth partitions. The three subsequent plotting commands target this wide axes box.
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.12
Figure 2.13
1. The vector V is given by V=[2 8 7 3 1 0 8 9]. Write down a single Matlab instruction to
produce a vector that contains 1 in the place of the odd numbers and -1 in the place of
the even numbers.
3.
4. Plot the following signal with FS=100 HZ.