You are on page 1of 16

140010741014

Tutorial - III

Tutorial III
Q.1

What is an accumulator? Write a program to implement it.

Ans: An accumulator is a register for short-term, intermediate storage of arithmetic and logic data
in a computer's CPU (central processing unit). The term "accumulator" is rarely used in reference to
contemporary CPUs, having been replaced around the turn of the millennium by the term "register."
In a modern computers, any register can function as an accumulator.
The most elementary use for an accumulator is adding a sequence of numbers. The
numerical value in the accumulator increases as each number is added, exactly as it happens in a
simple desktop calculator (but much faster, of course). Once the sum has been determined, it is
written to the main memory or to another register.
The term "accumulator" is used in a wide variety of noncomputing applications and
activities, such as electrical engineering (an energy storage device such as a rechargeable battery or
ultracapacitor), hydraulics (a mechanical energy storage device), in stock trading (a contract or
agreement), and even in gambling (a parlay bet).
Program:-

xl

Input X[n] =

l=0

Output Y[n] = X[n-1] + Xn


n1

Q.2

xl
l=0

+ Xn.

What is a Moving average filter? Write a program to implement for one dimensional
signal and two dimensional signal.

Ans: In statistics, a moving average (rolling average or running average) is a calculation to


analyze data points by creating a series of averages of different subsets of the full data set. It is also
called a moving mean (MM) or rolling mean and is a type of finite impulse response filter.

1)

Simple moving average :

In financial applications a simple moving average (SMA) is the unweighted mean of the
previous n data. However, in science and engineering the mean is normally taken from an equal

140010741014
Tutorial - III

number of data on either side of a central value. This ensures


that variations in the mean are aligned with the variations in the
data rather than being shifted in time. An example of a simple
equally weighted running mean for a n-day sample of closing
price is the mean of the previous n days' closing prices. If those
prices are
then the formula is

When calculating successive values, a new value comes into the sum and an old value drops
out, meaning a full summation each time is unnecessary for this simple case,

The period selected depends on the type of movement of interest, such as short, intermediate,
or long-term. In financial terms moving-average levels can be interpreted as support in a falling
market, or resistance in a rising market.
If the data used are not centered around the mean, a simple moving average lags behind the
latest datum point by half the sample width. An SMA can also be disproportionately influenced by
old datum points dropping out or new data coming in. One characteristic of the SMA is that if the
data have a periodic fluctuation, then applying an SMA of that period will eliminate that variation
(the average always containing one complete cycle). But a perfectly regular cycle is rarely
encountered.
For a number of applications, it is advantageous to avoid the shifting induced by using only
'past' data. Hence a central moving average can be computed, using data equally spaced on either
side of the point in the series where the mean is calculated. This requires using an odd number of
datum points in the sample window.
A major drawback of the SMA is that it lets through a significant amount of the signal shorter
than the window length. Worse, it actually inverts it. This can lead to unexpected artifacts, such as
peaks in the smoothed result appearing where there were troughs in the data. It also leads to the
result being less smooth than expected since some of the higher frequencies are not properly
removed.

2) Cumulative moving average:In a cumulative moving average, the data arrive in an ordered datum stream, and the user
would like to get the average of all of the data up until the current datum point. For example, an
investor may want the average price of all of the stock transactions for a particular stock up until the

140010741014
Tutorial - III

current time. As each new transaction occurs, the average price at the time of the transaction can be
calculated for all of the transactions up to that point using the cumulative average, typically an
equally weighted average of the sequence of n values
up to the current time:

The brute-force method to calculate this would be to store all of the data and calculate the
sum and divide by the number of datum points every time a new datum point arrived. However, it is
possible to simply update cumulative average as a new value,
becomes available, using the
formula:

Thus the current cumulative average for a new datum point is equal to the previous
cumulative average, times n, plus the latest datum point, all divided by the number of points
received so far, n+1. When all of the datum points arrive (n = N), then the cumulative average will
equal the final average.
The derivation of the cumulative average formula is straightforward. Using

and similarly for n + 1, it is seen that

Solving this equation for

results in:

3) Weighted moving average


A weighted average is any average that has multiplying factors to give different weights to data
at different positions in the sample window. Mathematically, the moving average is the convolution
of the datum points with a fixed weighting function. One application is removing pixelisation from a
digital graphical image.

140010741014
Tutorial - III

In technical analysis of financial data, a weighted moving


average (WMA) has the specific meaning of weights that decrease in
arithmetical progression. In an n-day WMA the latest day has weight
n, the second latest n 1, etc., down to one.

The denominator is a triangle number equal to


denominator will always be the sum of the individual weights.

In the more general case the

When calculating the WMA across successive values, the difference between the numerators
of WMAM+1 and WMAM is npM+1 pM pMn+1. If we denote the sum pM + + pMn+1 by TotalM,
then

The graph at the right shows how the weights decrease, from highest weight for the most recent
datum points, down to zero. It can be compared to the weights in the exponential moving average
which follows.

4) Exponential moving average


An exponential moving average (EMA), also known as an
exponentially weighted moving average (EWMA), is a type of
infinite impulse response filter that applies weighting factors
which decrease exponentially. The weighting for each older
datum decreases exponentially, never reaching zero. The graph
at right shows an example of the weight decrease.
The EMA for a series Y may be calculated recursively:

140010741014
Tutorial - III

for
Where:

The coefficient represents the degree of weighting decrease, a constant smoothing factor
between 0 and 1. A higher discounts older observations faster.

Yt is the value at a time period t.

St is the value of the EMA at any time period t.

S1 is undefined. S1 may be initialized in a number of different ways, most commonly by setting S1 to


Y1, though other techniques exist, such as setting S1 to an average of the first 4 or 5 observations.
The importance of the S1 initialisations effect on the resultant moving average depends on ; smaller
values make the choice of S1 relatively more important than larger values, since a higher
discounts older observations faster.
Whatever is done for S1 it assumes something about values prior to the available data and is
necessarily in error. In view of this the early results should be regarded as unreliable until the
iterations have had time to converge. This is sometimes called a 'spin-up' interval. One way to assess
when it can be regarded as reliable is to consider the required accuracy of the result. For example, if
3% accuracy is required, initialising with Y1 and taking data after five time constants (defined above)
will ensure that the calculation has converged to within 3% (only <3% of Y1 will remain in the result
). Sometimes with very small alpha, this can mean little of the result is useful. This is analogous to
the problem of using a convolution filter (such as a weighted average) with a very long window.
This formulation is according to Hunter (1986). By repeated application of this formula for different
times, we can eventually write St as a weighted sum of the datum points Yt, as:

for any suitable k = 0, 1, 2, ... The weight of the general datum point

is

An alternate approach by Roberts (1959) uses Yt in lieu of Yt1:

This formula can also be expressed in technical analysis terms as follows, showing how the EMA
steps towards the latest datum point, but only by a proportion of the difference (each time):

140010741014
Tutorial - III

Expanding out
each time results in the following power series, showing how the
weighting factor on each datum point p1, p2, etc., decreases exponentially:

where

is

is

and so on

,
since

This is an infinite sum with decreasing terms.


The N periods in an N-day EMA only specify the factor. N is not a stopping point for the
calculation in the way it is in an SMA or WMA. For sufficiently large N, the first N datum points in
an EMA represent about 86% of the total weight in the calculation when

i.e.

simplified, tends to

The above discussion requires a bit of clarification. The sum of the weights of all the terms
(i.e., infinite number of terms) in an exponential moving average is 1. The sum of the weights of
terms is
. Both of these sums can be derived by using the formula for the sum of
a geometric series. The weight omitted after
terms is given by subtracting this from 1, and you
get
(this is essentially the formula given below for the
weight omitted). Note that there is no "accepted" value that should be chosen for although there
are some recommended values based on the application. In the above discussion, we have

140010741014
Tutorial - III

substituted a commonly used value for


in the formula for the weight of terms.
This value for comes from setting the average age of the data from a SMA equal to the average
age of the data from an EMA and solving for . Again, it is just a recommendationnot a
requirement. If you make this substitution, and you make use of
then you get the 0.864 approximation. Intuitively, what this is telling us is that the weight after
terms of an `` -period" exponential moving average converges to 0.864.

The power formula above gives a starting value for a particular day, after which the
successive days formula shown first can be applied. The question of how far back to go for an initial
value depends, in the worst case, on the data. Large price values in old data will affect on the total
even if their weighting is very small. If prices have small variations then just the weighting can be
considered. The weight omitted by stopping after k terms is

which is
i.e. a fraction

out of the total weight. For example, to have 99.9% of the weight, set above ratio equal to 0.1% and

solve for k:

terms should be used. Since


approximately[12]

approaches

for this example (99.9% weight).

as N increases, this simplifies to

140010741014
Tutorial - III

5) Modified moving average:A modified moving average (MMA), running moving average (RMA), or smoothed moving
average (SMMA) is defined as:

In short, this is an exponential moving average, with

6) Application to measuring computer performance:Some computer performance metrics, e.g. the average process queue length, or the average CPU
utilization, use a form of exponential moving average.

Here is defined as a function of time between two readings. An example of a coefficient


giving bigger weight to the current reading, and smaller weight to the older readings is

where exp() is the exponential function, time for readings tn is expressed in seconds, and
is the period of time in minutes over which the reading is said to be averaged (the mean lifetime of
each reading in the average). Given the above definition of , the moving average can be expressed
as

For example, a 15-minute average L of a process queue length Q, measured every 5 seconds (time
difference is 5 seconds), is computed as

Q.3

Explain conversion from linear convolution to circular convolution with suitable


example.

140010741014
Tutorial - III

Ans: A convolution is an integral that expresses the amount of overlap of one function as it is
shifted over another function. It therefore blends one function with another. English dictionary
defines convolution as the action of coiling or twisting or winding together. Convolution is used
in filtering, Study of heat conduction, wave motion and other areas of physics and mathematics.
Convolution is relationship among three signals : input signal, impulse response and output
signal. If we know impulse response of any system then by using convolution we know output of the
system for every input signal.
How Convolution is calculated ?
If there are two signals , if we multiply them in time domain then we are really convolving them in
frequency domain and vice-versa.
There are Two types of convolution:
1. Linear convolution :
if

is length of input signal and

is length of impulse response h then length of linear

convolution is given by
2. Circular convolution :
if

is length of input signal and

is length of impulse response h then length of circular

convolution is given by
circular convolution. Following two are widely used :

. There are many methods of calculating

i) Circular convolution using linear convolution : Compute linear convolution and then
bring last

terms from

Example:

1
2
3
4
5
6
7

| 1 2 3
-|---------4| 4 8 12
|
5| 5 10 15
|
6| 6 12 18

and add them to first

terms of

140010741014
Tutorial - III

Note that
. To covert this linear convolution to circular
convolution we will add last 2 terms of Y_lc to first 2 terms.

Note that
This property of circular convolution is widely used in OFDM transcievers where CP used to avoid
inter symbol interference. Instead of adding zeros between two OFDM symbols, N1-1 last terms are
added at the start of symbol.
ii) Circular convolution using DFT multiplication:
See octave example below:
01
02
03
04
05
06
07
08

%%
%%
%%
%%

%% x = signal vector x[n]


%% h = signal vector h[n]
n= number of points in the circular convolution
Y_cc = result: n-point circular convolution of x and h
Y_lc = result: n-point linear convolution of x and h
using dft multiplication.

%%The circular convolution of x[n] and h[n] is equivalent to the %


%multiplication of their DFTs, i.e., x_fft y_fft.

09
10
x = [1,2,3];
11
h = [4,5,6];
12
13
x_fft = fft(x);
14
y_fft = fft(h)
15
Y_cc = ifft(x_fft.*y_fft)
16
17
subplot(211);
18
stem(Y_cc);
19 title('Circular convolution using DFT multiplication');

140010741014
Tutorial - III
20 grid on;
21
22
23 %One other thing to notice is that if we add N1-1 zeros at the end of x and h
24 %then we will get output as linera convolution
25
26
27
28
29
30
31
32
33
34
35
36
37
38

N1=length(x); N2=length(h);
X=[x,zeros(1,N2)];
Y=[h,zeros(1,N1)];
n=N1+N2-1;
x_fft = fft(X,n);
y_fft = fft(Y,n);
Y_lc = ifft(x_fft.*y_fft,n)
subplot(212);
stem(Y_lc);
title('Linear convolution');
grid on;

Output plot below shows that output :

140010741014
Tutorial - III

Q.4

Explain linear filtering using FFT.

Ans: FFT is a fast algorithm for execution of DFT. The multiplication of two DFTs is equivalently
a circular convolution of the two sequences. We can make use of DFT for computation of
convolution as the circular convolution in time domain is equivalently a multiplication in the DFT
domain using a property of convolution. We need to convert the linear convolution to a circular
convolution and then make use of FFT algorithm to reduce the number of computation for filtering.
The procedure to convert the linear convolution to a circular convolution is increase length of each
sequence by appending zeros so that it is equal to the length of the resulting convolved sequence.

Overlap-add Algorithm :Step 1: Take the L samples of data sequence . Append M 1 extra zeros to this block of data so
that its length is L + M 1.
Step 2: Append L 1 extra zeros to the FIR filter so that its length is L + M 1.
Step 3: Convolve the two sequences circularly using FFT as shown in Fig. 1 to obtain the output as
L + M - 1.
Step 4: Repeat steps 1 to 3 for all the blocks.
Step 5: Accumulate the results by adding the overlapped samples of the convolved output for

For example, let the input sequence be = [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]. Let the FIR filter
sequence be = [3 2 1 1]. Let us calculate the convolution using overlap-add method. We will use
block size for the data as L = 5 and M = 4. We have to append three zeros to the data block and 4
zeros to . Therefore, the length of both the sequences is L + M 1 = 5 + 4 1 = 8. When we use
FFT, N is selected as some exponential of 2. The data blocks will be
X1 = [ 1 2 3 4 5 0 0 0 ]
X2 = [ 1 2 3 4 5 0 0 0 ]
X3 = [ 1 2 3 4 5 0 0 0 ]
=[32110000]

140010741014
Tutorial - III

Overlap-Save Algorithm :The algorithm for overlap-save method can be written as follows:
Step 1: Take L samples of the data sequence .
Step 2: Append M 1 last sample from the first block at the start of the second block and so on so
that the length of each block is L + M 1.
Step 3: For the first block, there is no previous block, append zeros at the start of this block.
Step 4: Append L 1 extra zeros to the FIR of the filter so that its length is L + M 1.
Step 5: Convolve the two sequences circularly using FFT to obtain the output.
Step 6: Repeat steps 1 to 5 for all the blocks.
Step 7: Accumulate the result by appending samples of the convolved output for successive blocks.
Discard first M 1 samples from result of every block and then append M 1 samples of the first
block at the end of the accumulated result by circular shifting.
Step 8: Overlap the input data samples and save the rest of the non-overlapping result samples.
For example, let the input sequence be = [1 2 3 4 5 1 2 3 4 5 1 2 3 4 5].Let the FIR filter sequence
be = [3 2 1 1]. Let us calculate the convolution using overlap-save method. We will use block
size for the data as L = 5 and M = 4. We have to append three zeros to the data block and 4 zeros to
. Therefore, the length of both the sequences is L + M 1 = 5 + 4 1 = 8. When we use FFT, N
is selected as some exponential of 2. The data blocks will be
X1 = [0 0 0 1 2 3 4 5]
X2 = [3 4 5 1 2 3 4 5]
X3 = [3 4 5 1 2 3 4 5]
= [3 2 1 1 0 0 0 0]

140010741014
Tutorial - III

Q.5

Explain DTMF detection using Goertzel algorithm.

Ans:

Introduction:
Pulse dialing and tone dialing are the two systems used in telephone signaling. DTMF is the
name used for push-button tone dialing. DTMF uses two tones to represent each key on the touch
pad. DTMF has found widespread applications such as in automated telephone inquiry services
where a user can select options from a menu by sending DTMF signals from a telephone.
DTMF signaling represents a digit or a character by the sum of a high-frequency tone and a
low frequency tone (thereby, dual tone). When any key is pressed, the tone of the column and the
tone of the row are generated. The DTMF corresponding to that key is generated by either table
lookup or a resonator. The signal is transmitted through the telephone line to a switching center. The
switching center samples the signal and uses FFT or Goertzel algorithm to determine the received
frequencies and hence tell the corresponding digit.

Methodology:
Initially, a simple energy threshold is used to separate the digits in each file which can be done
as follows. The whole signal is divided into frames whose individual average energy is calculated.
The average of all these is considered as the threshold. But in some cases, the noise energy exceeds
the threshold. Hence, five continuous frames are joined to make the noise energy less than the signal

140010741014
Tutorial - III

energy. This is done in steps 1 and 2 of the program. Now, the starting point of each digit is needed
to be found, shown in step 3. If the current frame number is 1 and the energy is more than the
threshold, then it is one of the digits starting points. For frame numbers after 1, if the frame energy
is more than the threshold and also the previous frame energy, then it is again one of the digits
starting points. Then the Goertzel algorithm is implemented to obtain the frequency spectrum. Using
the largest two amplitudes in the output of Goertzel algorithm, the digits in the signal are found.

Understanding FFT And Goertzel Algorithm:


In this as we have seen when a key is pressed a signal with corresponding two frequencies are
transmitted. Now in order to carry out its functions and process the users request, the public
telephone system should first identify the dialed numbers. For this it carries the DTF and obtains the
digital frequency spectrum. Depending upon the frequency content it identifies the number. The
equations of DFT pair are shown below in equations (1) and (2):

A. Working of the FFT algorithm:


The FFT algorithm requires only 2Nlog 2N real multiplications. Using the same example with
N=1024 and the computation speed as 40MIPS, the total number of multiplications is just 20480,
and it takes 0.000512 seconds; which is a far better performance than 0.1ms in the case earlier.
B. Working of the Goertzel Algorithm:
Goertzel algorithm[ is especially useful when the number of frequency points needed to compute
are less than N. If we have to evaluate M number of DFT coefficients, where M< 2Nlog 2N the
number of real multiplications required for this algorithm is nearly equal to M(N+2)~MN. With our
typical values of N=1024 and M=7, the total number of multiplications needed is 7148, which takes
about 0.0001787 seconds; undoubtedly, the best among the three. By using the Goertzel method,
maximum time utilization is obtained and the fastest response obtained. The equations of the
Goertzel algorithm are:

140010741014
Tutorial - III

Observations:
Not many frequency points are required to decode the signal
1) If the Goertzel Algorithm criteria is satisfied, the time for computing is greatly reduced.
2) It is better than the FFT method.