DSP-Manual - R19
DSP-Manual - R19
1. Circular Convolution
2. Discrete Fourier Transform / Inverse Discrete Fourier Transform
3. Power Density Spectrum
4. Implementation of Filters using IIR
5. Implementation of Filters using FIR
6. Generation of Sinusoidal signal through filtering
7. Generation of DTMF Signals
8. Implementation of Decimation and Interpolation processes, I/D sampling Rate Converters.
1
Experiments performed using MATLAB
1 Circular Convolution 3
2
1. CIRCULAR CONVOLUTION
Purpose of Experiment
x[n] and h[n] are two finite sequences of length N with DFTs denoted by X[k] and H[k],
respectively. Let us form the product
W [k] = X[k]H[k],
and determine the sequence w[n] of length N for which the DFT is W [k].
First, extend x[n] and h[n] to periodic sequences with period N, x˜[n] and h˜[n], respectively.
Then, the periodic convolution of x˜[n] and h˜[n] corresponds to multiplication of the
corresponding periodic sequences of Fourier series coefficients
N−1
~ ~ ~ ~ ~
w [ n ]= ∑ ~
x [n] h [n−m] W [ k ] = X [ k ] H [k ]
m=0
~
The periodic sequence ~ w [ n ] for which the DFS coefficients are W [ k ] corresponds to the
periodic extension of the finite length sequence w[n] with period N. We can recover w[n] by
extracting one period of ~
w [ n ]:
W[n] = ~
w [ n ] RN [n]
N−1
= ∑ ~x [ m ] ~h[n−m]
m =0
N−1
= ∑ x [ m ] h[((n−m))¿¿ N ]¿
m =0
Let x1(n) and x2(n) be two given sequences. The steps followed for circular convolution of
x1(n) and x2(n) are
3
Take two concentric circles. Plot N samples of x1(n) on the circumference of the
outer circle maintaining equal distance between successive points in anti-clockwise
direction.
For plotting x2(n), plot N samples of x2(n) in clockwise direction on the inner circle,
starting sample placed at the same point as 0th sample of x1(n)
Multiply corresponding samples on the two circles and add them to get output.
Rotate the inner circle anti-clockwise with one sample at a time.
Matrix method represents the two-given sequence x1(n)x1(n) and x2(n)x2(n) in matrix form.
One of the given sequences is repeated via circular shift of one sample at a time to
form a N X N matrix.
The other sequence is represented as column matrix.
The multiplication of two matrices gives the result of circular convolution.
Experimental Steps
ALGORITHM:
Step 1: Start
Step 2: Read the first sequence
Step 3: Read the second sequence
Step 4: Find the length of the first sequence
Step 5: Find the length of the second sequence
Step 6: Make the length of both sequences equal if they are having different length by
padding
zeros at last.
Step 7: Perform circular convolution by using matrix multiplication
Step 8: Display the output sequence
Step 9: Stop
4
STAR
STOP
Sample Code
clc; clear all; close all
x=[1 2 3 4]; % Input Sequence
h=[4 3 2 1]; % Impulse response sequence
n=length(x);
c=zeros(1,n);
h1=[h(1) h(end:-1:2)];
for i=1:n
c(i)=x*h1';
h1=[h1(end) h1(1:end-1)];
end
%verification
x1=fft(x,n);
h2=fft(h,n);
p=x1.*h2;
c1=ifft(p);
c1
Expected Outputs
14 16 14 16
5
c1 = 14 16 14 16
− j2 πnk
Where W nk
N = e N = 𝑇𝑤𝑖𝑑𝑑𝑙𝑒 𝑓𝑎𝑐𝑡𝑜r
Experimental Steps
Step 1: Start
Step 3: Find the DFT of the input sequence using direct equation of DFT.
Step 5: Plot DFT and IDFT of the given sequence using matlab command stem.
Step: Stop
6
STAR
STOP
Sample Code
%Program to find dft and idft for a given DT signal
clear all;
clc;
x=[1 1 1 1 1 1 1 1]; %Input
Xk=dft(x); %calling function
xhat=idft(Xk); %calling function
subplot(2,1,1)
stem(1:length(x),x)
xlabel('time');
ylabel('amplitude');
title('input signal');
subplot(2,1,2)
stem(1:length(xhat),xhat)
xlabel('time');
ylabel('magnitude');
title('reconstructed signal');
function Xk=dft(x)
7
[N,M]=size(x);
if M~=1
x=x';
N=M;
end
Xk=zeros(N,1);
n=1:N
for k=1:N
Xk(k)=exp(-j*2*pi*k*n/N)*x;
end
%Calling function for idft
function[x]=idft(Xk)
[N,M]=size(Xk);
if M~=1
Xk=Xk';
N=m;
end
x=zeros(N,1);
k=1:N;
for n=1:N
x(n)=exp(j*2*pi*k*n/N)*Xk;
end
x=x/N;
Expected Outputs
8
input signal
1
amplitude
0.5
0
1 2 3 4 5 6 7 8
time
reconstructed signal
1
magnitude
0.5
0
1 2 3 4 5 6 7 8
time
Experimental Steps
Algorithm
9
Step 1: Start
Step 2: Get the input sequence.
Step 3: Find the DFT of the input sequence using direct equation of DFT.
Step 4: Obtain complex component DFT sequence.
Step 5: Compute PSD using the product of DFT and its complex component
Step 6: Plot PSD of the given sequence using Matlab command stem.
Step 7: Display the above outputs.
Step: Stop STAR
STOP
Sample Code
%implementation of FFT of a given signal or sequence
clear all;
clc;
fs=8000;
f1=1000;
f2=2000;
N=200;
n=0:N-1;
x=sin(2*pi*f1*n/fs)+2*sin(2*pi*2000*n/fs);
xf=abs(fft(x))/N;
power_two=xf.*xf;
f=(0:N-1)*fs/N;
subplot(2,1,1);
plot(f,xf);
xlabel('frequency in hz');
ylabel('magnitude of the spectrum');
10
title(['two sided magnitude spectrum at' , num2str(f1),' and at',num2str(f2),'with
reference to ',num2str(fs/2)]);
subplot(2,1,2);
plot(f,power_two);
xlabel('frequency in hz');
ylabel('power spectrum');
title(['two sided power spectrum at' , num2str(f1),' and t',num2str(f2),'with reference
to ',num2str(fs/2)]);
Expected Outputs
11
two sided magnitude spectrum at1000 and at2000with reference to 4000
0.5
0
0 1000 2000 3000 4000 5000 6000 7000 8000
frequency in hz
two sided power spectrum at1000 and t2000with reference to 4000
1
power spectrum
0.5
0
0 1000 2000 3000 4000 5000 6000 7000 8000
frequency in hz
12
Expected Outputs
1.5
0.5
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
one sided magnitute spectrum at1000ana at2000with reference to 4000
4
power spectrum
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
13
4. Implementation of IIR filter for a given input
sequence
Purpose of Experiment
The following parameters are commonly used to specify the frequency response:
2
ϵ : pass band ripple parameter.
δ p: passband deviations
δ s: stop band deviations
f p 1 and f p 2: Passband edge frequencies
f s 1and f s 2: Stopband edge frequencies
The band edge frequencies are sometimes given in normalized form, i.e., a fraction of the sampling
f
frequency .
Fs
Passband and stopband deviations many be expressed as ordinary numbers or in decibels: The
passband ripples in decibels is
A s=−20 log 10 (δ s )
Experimental Steps
1. Determining specifications
2. Design Analog filter according to the specifications.
-Butterworth, Chebyshev, Elliptic, etc.
3. Design digital filter from analog prototypes.
-Impulse invariance, Bilinear transformation, etc.
4. Choosing a realization structure.
- Direct form, Cascade, Parallel, Transpose or Lattice form
5. Implementing the filter-Software implementation (such as MATLAB or C code) or Hardware
implementation (such as a DSP, a microcontroller, or an ASIC)
The rationale behind the design of IIR filter is that there already exist a wealth of information on
analog filters in the literature, which can be utilized. There are two common approaches used to
convert analog filters into equivalent digital filters: (1) Impulse invariant and bilinear transformation
methods. In above design steps, step 3. Implements the mapping from analog to digital.
14
Sample Code
clear all;
close all;
clc;
%fs=input('enter the sampling frequency which is greater than twice the max');
fs=8000;
N=1024;
M=2;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
%Spectrum of Input
X=2*abs(fft(x,N))/N;
X(1)=X(1)/2;
f=(0:1:N/2-1)*fs/N;
subplot(3,1,1);
plot(f,X(1:N/2));
xlabel('frequency in hz');
ylabel('magnitude');
title(['spectrum of input signal at',num2str(f1), 'and at',num2str(f2)]);
Expected Outputs
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
butterworth filter of order5
magnitude in db
-100
-200
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
Filtered Signal at frequencies above1500
1
magnitude
0.5
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
B.) Implementation of Low Pass IIR filter for a given input sequence
clear all;
close all;
clc;
16
%fs=input('enter the sampling frequency which is greater than twice the max');
fs=8000;
N=1024;
M=2;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
%Spectrum of Input
X=2*abs(fft(x,N))/N;
X(1)=X(1)/2;
f=(0:1:N/2-1)*fs/N;
subplot(3,1,1);
plot(f,X(1:N/2));
xlabel('frequency in hz');
ylabel('magnitude');
title(['spectrum of input signal at',num2str(f1), 'and at',num2str(f2)]);
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
butterworth filter of order5
magnitude in db
-100
-200
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
18
5. Implementation of FIR filter for a given input
sequence
Purpose of Experiment
N−1
H [ z ]= ∑ h ( k ) z−k (2)
k=0
Where, h [ k ] , k=0 ,1 , 2 ….. N−1 are the impulse response coefficients of the filter, H ( z ) is
the transfer function of the filter, and N is the filter length.
The output y [ n ] is a function of only past and present values of input x [n]. When FIR filter
are implemented in this form, i.e. by direct evaluation of eq. 1, they are always stable. Eq. 2 is the
transfer function of the filter. It provides a means of analysing the filter, for example evaluating the
frequency response.
Experimental Steps
∑ b k z−k
k=0
H ( z )= N−1
∑ a k z −k
k=0
This step calculates the coefficients, a k and b k
3. Choosing a realization structure.
- Direct for I, II, Cascade, Parallel, Transpose or Lattice form
4. Implementing the filter-Software implementation (such as MATLAB or C code) or Hardware
implementation (such as a DSP, a microcontroller, or an ASIC)
19
Sample Code
%program for filtering unwanted low frequency signals using FIR filters
clear all;
close all;
clc;
fs=8000;
n=0:511;
f1=500;f2=1500;f3=2000;
x=sqrt(2)*sin(2*pi*f1*n/fs)+sqrt(3)*sin(2*pi*f2*n/fs)+2*sin(2*pi*f3*n/fs);
subplot(2,1,1);
plot(n,x);
xlabel('time,n');
ylabel('magnitude of input signal');
title('three tone input sinusoidal signal');
N=length(x);
Mag_x=2*abs(fft(x))/N;
Mag_x(1)=Mag_x(1)/2;
f_Hz=(1:N/2)*fs/N;
subplot(2,1,2);
20
plot(f_Hz,Mag_x(1:N/2));
xlabel('frequency in hz');
ylabel('magnitude of input signal spectrum');
title(['three tone input signal spectrum at frequencies ',num2str(f1),'',num2str(f2),'& at
',num2str(f3)]);
wc_L=2*pi*500/fs;
wc_H=0;
B=firwd(133,1,wc_L,wc_H,4); %Ftype=1 for LPF
filt_x=filter(B,1,x);
figure(2)
subplot(2,1,1);
plot(n,filt_x);
xlabel('time,n');
ylabel('magnitude of filtered signal');
title('magnitude of filtered sinusoidal signal');
Mag_x=2*abs(fft(filt_x))/N;
Mag_x(1)=Mag_x(1)/2;
f_Hz=(1:N/2)*fs/N;
subplot(2,1,2);
plot(f_Hz,Mag_x(1:N/2));
xlabel('frequency in hz');
ylabel('magnitude of filtered signal spectrum');
title('LP signal spectrum');
21
Expected Outputs
-5
0 100 200 300 400 500 600
time,n
magnitude of input signal spectrum
1.5
0.5
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
22
magnitude of filtered sinusoidal signal
0.5
-0.5
-1
0 100 200 300 400 500 600
time,n
LP signal spectrum
0.6
0.4
0.2
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
B). Implementation of High Pass FIR filter for a given input sequence
%program for filtering unwanted low frequency signals using FIR filters
clear all;
close all;
clc;
fs=8000;
n=0:511;
f1=500;f2=1500;f3=2000;
x=sqrt(2)*sin(2*pi*f1*n/fs)+sqrt(3)*sin(2*pi*f2*n/fs)+2*sin(2*pi*f3*n/fs);
subplot(2,1,1);
plot(n,x);
xlabel('time,n');
ylabel('magnitude of input signal');
title('three tone input sinusoidal signal');
N=length(x);
Mag_x=2*abs(fft(x))/N;
Mag_x(1)=Mag_x(1)/2;
f_Hz=(1:N/2)*fs/N;
subplot(2,1,2);
plot(f_Hz,Mag_x(1:N/2));
23
xlabel('frequency in hz');
ylabel('magnitude of input signal spectrum');
title(['three tone input signal spectrum at frequencies ',num2str(f1),'',num2str(f2),'& at
',num2str(f3)]);
wc_H=2*pi*1500/fs;
wc_L=0;
B=firwd(133,2,wc_L,wc_H,4); %Ftype=2 for HPF
filt_x=filter(B,1,x);
figure(2)
subplot(2,1,1);
plot(n,filt_x);
xlabel('time,n');
ylabel('magnitude of filtered signal');
title('magnitude of filtered sinusoidal signal');
Mag_x=2*abs(fft(filt_x))/N;
Mag_x(1)=Mag_x(1)/2;
f_Hz=(1:N/2)*fs/N;
subplot(2,1,2);
plot(f_Hz,Mag_x(1:N/2));
xlabel('frequency in hz');
ylabel('magnitude of filtered signal spectrum');
title('HP signal spectrum');
Expected Outputs
-5
0 100 200 300 400 500 600
time,n
magnitude of input signal spectrum
1.5
0.5
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
25
magnitude of filtered signal spectrum magnitude of filtered signal magnitude of filtered sinusoidal signal
4
-2
-4
0 100 200 300 400 500 600
time,n
HP signal spectrum
2
1.5
0.5
0
0 500 1000 1500 2000 2500 3000 3500 4000
frequency in hz
26
6. Generation of Sinusoidal signal through filtering
Purpose of Experiment
y [ n ] =h [ n ] =sin ω0 n
Algorithm:
Step1:Given
y [ n ] =h [ n ] =sin ω0 n
Step 2: Find Y [ z ]
−1
sin ω 0 z
Y [ z ]= −1 −2
1−2cos ω0 z + z
∑ b k z−k
k=0
H ( z )= N−1
∑ a k z −k
k=0
Experimental Steps
27
Sample Code
clear all;
close all;
clc;
%fs=input('enter the sampling frequency of the signal=');
%f_hz=input('enter the signal frequency in Hz= ');
f_hz=1000;
fs=8000;
f0=2*pi*f_hz/fs;
a1=2*cos(f0);
a2=-1;
b1=sin(f0);
%xnm1=zeros(1,128);
xnm1=1;
ynm1=0;
ynm2=0;
%Initializing output to zero
%output=zeros(1,200);
y1=zeros(1,200);
for i=1:200
y1(i)=b1*xnm1+a1*ynm1+a2*ynm2;
ynm2=ynm1;
28
ynm1=y1(i);
xnm1=0;
%output(i)=y1(i);
end
subplot(2,1,1)
plot(1:length(y1),y1);
xlabel('Time in Seconds');
ylabel('Amplitude of the signal');
title(['sinusoidal signal of frequency',num2str(f_hz),'Hz']);
Ak=2*abs(fft(y1))/length(y1);
Ak(1)=Ak(1)/2;
f=(0:1:(length(y1)-1)/2)*fs/length(y1);
subplot(2,1,2)
plot(f, Ak(1:(length(y1)+1)/2));
xlabel('Frequency in Hz');
ylabel('Magnitude of the Spectrum');
title(['Spectrum at',num2str(f_hz),'Hz frequency']);
Expected Outputs
0.5
-0.5
-1
0 20 40 60 80 100 120 140 160 180 200
Time in Seconds
Spectrum at1000Hz frequency
Magnitude of the Spectrum
0.5
0
0 500 1000 1500 2000 2500 3000 3500 4000
Frequency in Hz
29
7. Generation of DTMF Signals
Purpose of Experiment
The DTMF stands for “Dual Tone Multi Frequency”, and is a method of representing digits with tone
frequencies, in order to transmit them over an analog communications network, for example a
telephone line. In telephone networks, DTMF signals are used to encode dial trains and other
information. Dual-tone Multi-Frequency (DTMF) signalling is the basis for voice communications
control and is widely used worldwide in modern telephony to dial numbers and configure
switchboards. It is also used in systems such as in voice mail, electronic mail and telephone banking.
A DTMF signal consists of the sum of two sinusoids - or tones - with frequencies taken from two
mutually exclusive groups. These frequencies were chosen to prevent any harmonics from being
incorrectly detected by the receiver as some other DTMF frequency. Each pair of tones contains one
frequency of the low group (697 Hz, 770 Hz, 852 Hz, 941 Hz) and one frequency of the high group
(1209 Hz, 1336 Hz, 1477Hz) and represents a unique symbol.
697 Hz 1 2 3
770 Hz 4 5 6
852 Hz 7 8 9
941 Hz * 0 #
Experimental Steps
STAR
30
INPUTS: Enter row and
column frequencies
STOP
Sample Code
clear all;
close all;
clc;
%Signal and Sampling frequency
fs=8000;
t=0:1/fs:1;
x=zeros(1,length(t));
x(1)=1;
%Calculation of output of LF signal generation
%fL=input('enter any one low frequency value from [697 770 852 941] %vector');
fL=852;
%Numerator coefficients of HL(z)
bL=[0 sin(2*pi*fL/fs)];
% Output of HL(z)
yL=filter(bL,aL,x);
31
%Denominator coefficients of HH(z)
aH=[1 -2*cos(2*pi*fH/fs) 1];
% Output of HH(z)
yH=filter(bH,aH,x);
subplot(2,1,2)
plot(f,Mag_Spec(1:(length(y_LH)+1)/2));
grid
xlabel('Frequency in Hz');
ylabel('Magnitude of the Spectrum');
title(['Magnitude Spectrum of DTMF Tone for Key',num2str(key),' at '
num2str(fL),'HZ & at',num2str(fH),'Hz']);
Expected Outputs
33
8. Implementation of Decimation and Interpolation
processes, I/D sampling Rate Converters
Purpose of Experiment
• The following figure shows a typical M-fold decimation filter, where M is the integer value
by which you want to decrease the sampling frequency. This filter contains a low pass FIR
filter H(z). This low pass FIR filter is an anti-aliasing filter followed by an M-fold decimator.
The decimator passes every Mth sample and discards the other samples. After this operation,
the decimation filter changes the sampling frequency fs of the input signal x(n) to a new
sampling frequency fs/M. The decimation filter then returns an output signal y(n) with the
new sampling frequency
y ( n )=x ( Mn ) ………………Decimation
x(n)
x(n)
Anti M
aliasing
filter H(z) fs/M
fs Decimator
To prevent aliasing, this system uses the low pass filter H(z) before the M-fold decimator to suppress
the frequency contents above the frequency fs/(2M), which is the Nyquist frequency of the output
signal. This system produces the same results as an analog anti-aliasing filter with a cutoff frequency
offs/(2M) followed by an analog-to-digital (A/D) converter with a sampling frequency offs/M.
Because the system shown in the figure above is in the digital domain, H(z) is a digital anti aliasing
filter.
Experimental Steps
Start
Expected Outputs
36
Original Input Signal
1
0.5
Amplitude
-0.5
-1
0 5 10 15 20 25 30 35 40 45 50
time index, n
Decimation Signal by a factor2
1
0.5
Amplitude
-0.5
-1
0 5 10 15 20 25 30 35 40 45 50
time index, n
Interpolation process
Up sampling is the process of inserting zero-valued samples between original samples to increase the
sampling rate. (This is called “zero-stuffing”.) “Interpolation”, is the process of up sampling followed
by filtering. The filtering removes the undesired spectral images. The primary reason to interpolate is
simply to increase the sampling rate at the output of one system so that another system operating at a
higher sampling rate can input the signal.
y(n)
x(n) Anti
L imaging
filter H(z)
Up sampler
-π/L ω
π/L
Multirate" means "multiple sampling rates". A multi rate DSP system uses multiple sampling rates
within the system. Whenever a signal at one rate has to be used by a system that expects a different
rate, the rate has to be increased or decreased, and some processing is required to do so. Therefore
"Multirate DSP" refers to the art or science of changing sampling rates.
n
y ( n )=x ( )………………..Interpolation
L
37
"Resampling" means combining interpolation and decimation to change the sampling rate by a
rational factor. Resampling is done to interface two systems with different sampling rates.
clear all;
close all;
clc;
% Generation of Input Signal
N=1024;
L=2;
fs=8000;
n=0:N-1;
x=2*sin(2*pi*1000*n/fs)+sin(2*pi*2000/fs*n);
%Upsampling process
%Upsampling by a factor of L
w=zeros(1,L*N);
for k=0:N-1
w(L*k+1)=x(k+1);
end
L_w=length(w);
W=2*abs(fft(w,L_w))/L_w;
W(1)=W(1)/2;
f=(0:L_w/2-1)*fs*L/L_w;
%Use FIR LPF with a cut off frequency of 2*pi*fc or 2*pi*3000 and a sampling rate
of L*fs
N=25;
new_fs=L*fs;
wcL=2*pi*2500/new_fs;
Ftype=1;
wcH=0;
wtype=4;
B=firwd(N,1,wcL,wcH,wtype);
% Interpolation operation
y=filter(B,1,w);
Y=2*abs(fft(y,L_w)/L_w);
Y(1)=Y(1)/2;
fsL=(0:L_w/2-1)*fs*L/L_w;
subplot(2,1,2)
stem(1:length(y(1:200)),y(1:200));
xlabel('time index, n','fontsize',12)
ylabel('Amplitude','fontsize',12)
title(['Interpolated Signal by a factor',num2str(L)],'fontsize',12)
% Plot of Spectrum
figure(2)
subplot(2,1,1)
plot(f,W(1:L_w/2));
xlabel('Frequency in Hz')
ylabel('Amplitude','fontsize',12)
title('Spectrum of Upsampled Signal','fontsize',12)
subplot(2,1,2)
plot(fsL,Y(1:L_w/2));
xlabel('Frequency in Hz')
ylabel('Amplitude','fontsize',12)
title('Spectrum of Interpolated Signal','fontsize',12)
39
Expected Outputs
Upsampled Signal
2
Amplitude
-1
-2
0 20 40 60 80 100 120 140 160 180 200
time index, n
Interpolated Signal by a factor2
1
Amplitude
0.5
-0.5
-1
0.5
0
0 1000 2000 3000 4000 5000 6000 7000 8000
Frequency in Hz
Spectrum of Interpolated Signal
1
Amplitude
0.5
0
0 1000 2000 3000 4000 5000 6000 7000 8000
Frequency in Hz
40
Implementation of I/D Sampling Rate Converters
• A Multirate DSP system uses multiple sampling rates within the system.
• Whenever a signal at one rate has to be used by a system that expects a different rate, the rate
has to be increased or decreased, and some processing is required to do so.
• Therefore "Multirate DSP" refers to the art or science of changing sampling rates.
• "Resampling" means combining interpolation and decimation to change the sampling rate by
a rational factor.
w(n)
v(n) y(n)
x(n)
L H(z) M
Fx
LFx LFx/M
clear all;
close all;
clc;
% Generation of Input Signal
N=1024;
L=3;
M=8;
fs=8000;
n=0:N-1;
x=5*sin(2*pi*1000*n/fs)+cos(2*pi*2500/fs*n);
%Upsampling process
%Upsampling by a factor of L
w=zeros(1,L*N);
for k=0:N-1
w(L*k+1)=x(k+1);
end
L_w=length(w);
W=2*abs(fft(w,L_w))/L_w;
W(1)=W(1)/2;
f=(0:L_w/2-1)*fs*L/L_w;
41
%Use FIR LPF with a cut off frequency of 2*pi*fc or 2*pi*3000 and a sampling rate
of L*fs
N=53;
new_fs=L*fs;
wcL=2*pi*1250/new_fs;
Ftype=1;
wcH=0;
wtype=4;
B=firwd(N,1,wcL,wcH,wtype);
%Down Sampling
d=y(1:M:L_w);
Ld=length(d);
D=2*abs(fft(d,Ld)/Ld);
D(1)= D(1)/2;
fsd=(0:Ld/2-1)*(fs*L/M)/Ld;
% Plot of Spectrum
subplot(3,1,1)
plot(f,W(1:L_w/2));
xlabel('Frequency in Hz')
ylabel('Amplitude','fontsize',12)
title('Spectrum of Upsampled Signal','fontsize',12)
subplot(3,1,2)
plot(fsL,Y(1:L_w/2));
xlabel('Frequency in Hz')
ylabel('Amplitude','fontsize',12)
title('Spectrum of Combined Signal','fontsize',12)
subplot(3,1,3)
plot(fsd,D(1:Ld/2));
xlabel('Frequency in Hz')
ylabel('Amplitude','fontsize',12)
title('Spectrum of Down sampled Signal','fontsize',12)
42
Expected Outputs
0
0 2000 4000 6000 8000 10000 12000
Frequency in Hz
Spectrum of Combined Signal
Amplitude
0.5
0
0 2000 4000 6000 8000 10000 12000
Frequency in Hz
Spectrum of Down sampled Signal
Amplitude
0.5
0
0 500 1000 1500
Frequency in Hz
Additional Experiments
clear all;
close all;
clc;
N=200;
f1=100;
f2=400;
fs=1000;
n=0:N-1;
x=3*sin(2*pi*f1*n/fs)+2*sin(2*pi*f2*n/fs);
L=25;
fc=400;
b=fir1(55,fc/fs);
y=filter(b,1,x);
subplot(3,1,1);
43
plot(n,x,n,y,'r');
xlabel('time,n');
ylabel('magnitude of input signal');
title('two input sinusoidal signal filtered');
N=length(x);
Mag_x=2*abs(fft(x))/N;
Mag_x(1)=Mag_x(1)/2;
f_Hz=(1:N/2)*fs/N;
subplot(3,1,2);
plot(f_Hz,Mag_x(1:N/2));
xlabel('frequency in hz');
ylabel('magnitude of input signal spectrum');
title('three tone input signal spectrum');
N=length(y);
Mag_y=2*abs(fft(y))/N;
Mag_y(1)=Mag_y(1)/2;
f_Hz=(1:N/2)*fs/N;
subplot(3,1,3);
plot(f_Hz,Mag_y(1:N/2));
xlabel('frequency in hz');
ylabel('magnitude of input signal spectrum');
44
Expected Outputs
0
spectrumof input signal spectrum
-5
0 20 40 60 80 100 120 140 160 180 200
time,n
three tone input signal spectrum
0
magnitude
0
0 50 100 150 200 250 300 350 400 450 500
frequency in hz
%To find frequency response of a given system given in Transfer function /Difference
Equation form
Clear Section
clear all;
close all;
clc;
45
subplot(2,1,1);
plot(w,Mag_H);
grid;
xlabel('Frequency in radians');
ylabel('Magnitude of frequency response');
title('Frequency response');
subplot(2,1,2);
plot(w,Phase_H);
grid;
xlabel('Frequency in radians');
ylabel('Phase response in degrees');
title('Phase response');
Expected Outputs
Magnitude of frequency response
Frequency response
1.5
0.5
0
0 0.5 1 1.5 2 2.5 3 3.5
Frequency in radians
Phase response in degrees
Phase response
100
50
-50
-100
0 0.5 1 1.5 2 2.5 3 3.5
Frequency in radians
46
TMS320C6713 DSP Starter Kit (DSK)
References
• The DSK boards can be ordered from the TI website: www.ti.com
Additional information and software downloads can be found on the Spectrum Digital
website: www.spectrumdigital.com
47
TMS320C6713 DSK KIT
DSP Board
FLOATING POINT C6713TM 255MHz DSP
16-bit stereo codec (stereo in & out)
Four 3.5 millimeter audio jacks
512K non-volatile Flash memory
16MB SDRAM
HPI, McASP & I2C interface header emulation
On-board standard JTAG interface
Plug and play JTAG support via USB connectors
+5-volt universal power supply
Expansion port for plug-in modules
48
TMS320C6713 DSK Overview Block Diagram
51
9. LINEAR CONVOLUTION
Purpose of Experiment
Sample Code
53
10. CIRCULAR CONVOLUTION
Purpose of Experiment
Steps for cyclic convolution are the same as the usual convolution, except all index
calculations
are done "mod N" = "on the wheel"
Step 2: "Spin" h[−m] n times Anti Clock Wise (counter-clockwise) to get h[n-m]
(i.e. Simply rotate the sequence, h[n], clockwise by n steps)
Step 3: Point wise multiply the f[m] wheel and the h[n−m] wheel. Sum=y[n]
Step 4: Repeat for all 0≤n≤N−1
Example 1: Convolve (n = 4)
h[−m] =
54
Multiply f[m] and sum to yield: y[0] =3
h[1−m]
Figure 5
Multiply f[m] and sum to yield: y[1] =5
h[2−m]
Figure 6
Multiply f[m] and sum to yield: y[2] =3
h[3−m]
Figure 7
Multiply f[m] and sum to yield: y[3] =1
Sample Code
#include<stdio.h>
#include<math.h>
int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30];
void main()
{
printf(" enter the length of the first sequence\n");
scanf("%d",&m);
printf(" enter the length of the second sequence\n");
scanf("%d",&n);
55
printf(" enter the first sequence\n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf(" enter the second sequence\n");
for(j=0;j<n;j++)
scanf("%d",&h[j]);
if(m-n!=0) /*If length of both sequences are not equal*/
{
if(m>n) /* Pad the smaller sequence with zero*/
{
for(i=n;i<m;i++)
h[i]=0;
n=m;
}
for(i=m;i<n;i++)
x[i]=0;
m=n;
}
y[0]=0;
a[0]=h[0];
for(j=1;j<n;j++) /*folding h(n) to h(-n)*/
a[j]=h[n-j];
/*Circular convolution*/
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
/*circular shift*/
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
for(i=0;i<n;i++)
{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}
}
/*displaying the result*/
printf(" the circular convolution is\n");
for(i=0;i<n;i++)
printf("%d \t",y[i]);
}
Expected Outputs
INPUT: Eg: x[4]={1, 2, 3,4} h[4]={1, 2, 3,4} OUT PUT y[4]={26, 28,
26,20}
56
11. Finite Impulse Response Filter(FIR)
Sample Code
57
Following are the steps to design linear phase FIR filters Using Windowing Method.
I. Clearly specify the filter specifications.
Eg: Order = 30;
Sampling Rate = 8000 samples/sec
Cut off Freq. = 400 Hz.
II. Compute the cut-off frequency Wc
Eg: Wc = 2*pie* fc / Fs
= 2*pie* 400/8000
= 0.1*pie
III. Compute the desired Impulse Response h d (n) using particular Window
Eg: b_rect1=fir1 (order, Wc , 'high',boxcar(31));
IV. Convolve input sequence with truncated Impulse Response x (n)*h (n)
B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter
oefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 < Wn < 1.0,
with 1.0 corresponding to half the sample rate. The filter B is real and has linear phase, i.e.,
even symmetric coefficients obeying B(k) =B(N+2-k), k = 1,2,...,N+1. If Wn is a two-
element vector, Wn = [W1 W2], FIR1 returns an order N bandpass filter with passband W1 <
W < W2. B = FIR1(N,Wn,'high') designs a highpass filter. B = FIR1(N,Wn,'stop') is a
bandstop filter if Wn = [W1 W2]. If Wn is a multi-element vector, Wn = [W1 W2 W3 W4
W5 ... WN], FIR1 returns an order N multiband filter with bands 0 < W < W1, W1 < W <
W2, ..., WN < W < 1. B = FIR1(N,Wn,'DC-1') makes the first band a passband. B =
FIR1(N,Wn,'DC-0') makes the first band a stopband. For filters with a passband near Fs/2,
e.g., highpass and bandstop filters, N must be even.
By default FIR1 uses a Hamming window. Other available windows, including Boxcar,
Hanning, Bartlett, Blackman, Kaiser and Chebwin can be specified with an optional trailing
argument. For example, B = FIR1(N,Wn,kaiser(N+1,4)) uses a Kaiser window with beta=4.
B = FIR1(N,Wn,'high',chebwin(N+1,R)) uses a Chebyshev window.
By default, the filter is scaled so the center of the first pass band has magnitude exactly one
after windowing. Use a trailing 'noscale' argument to prevent this scaling, e.g. B =
FIR1(N,Wn,'noscale'),
B = FIR1(N,Wn,'high','noscale'), B = FIR1(N,Wn,wind,'noscale').
% FIR Low pass filters using rectangular, triangular and kaiser windows
% sampling rate – 8000
order = 50;
cf=[500/4000,1000/4000,1500/4000]; cf--> contains set of cut-off frequencies[Wc ]
% cutoff frequency – 500
58
b_rect1=fir1(order,cf(1),boxcar(51)); %Rectangular
b_tri1=fir1(order,cf(1),bartlett(51)); %Triangular
b_kai1=fir1(order,cf(1),kaiser(51,8)); %Kaisar [Where 8-->Beta Co-efficient]
% cutoff frequency - 1000b_rect2=fir1(order,cf(2),boxcar(51));
b_tri2=fir1(order,cf(2),bartlett(51));
b_kai2=fir1(order,cf(2),kaiser(31,8));
% cutoff frequency - 1500
b_rect3=fir1(order,cf(3),boxcar(51));
b_tri3=fir1(order,cf(3),bartlett(51));
b_kai3=fir1(order,cf(3),kaiser(51,8));
fid=fopen('FIR_lowpass_rectangular.txt','wt');
fprintf(fid,'\t\t\t\t\t\t%s\n','Cutoff -400Hz');
fprintf(fid,'\nfloat b_rect1[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect1);
fseek(fid,-1,0);
fprintf(fid,'};');
fprintf(fid,'\n\n\n\n');
fprintf(fid,'\t\t\t\t\t\t%s\n','Cutoff -800Hz');
fprintf(fid,'\nfloat b_rect2[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect2);
fseek(fid,-1,0);
fprintf(fid,'};');
fprintf(fid,'\n\n\n\n');
fprintf(fid,'\t\t\t\t\t\t%s\n','Cutoff -1200Hz');
fprintf(fid,'\nfloat b_rect3[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect3);
fseek(fid,-1,0);
fprintf(fid,'};');
fclose(fid);
winopen('FIR_highpass_rectangular.txt');
T.1 : Matlab generated Coefficients for FIR Low Pass Kaiser filter:
fir.c
#include "xyzcfg.h"
#include "dsk6713.h"
#include "dsk6713_aic23.h"
float filter_Coeff[] ={0.000000,-0.001591,-0.002423,0.000000,0.005728,
59
0.011139,0.010502,-0.000000,-0.018003,-0.033416,-0.031505,0.000000,
0.063010,0.144802,0.220534,0.262448,0.220534,0.144802,0.063010,0.000000,
-0.031505,-0.033416,-0.018003,-0.000000,0.010502,0.011139,0.005728,
0.000000,-0.002423,-0.001591,0.000000 };
static short in_buffer[100];
DSK6713_AIC23_Config config = {\
0x0017, /* 0 DSK6713_AIC23_LEFTINVOL Leftline input channel volume */\
0x0017, /* 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume*/\
0x00d8, /* 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume */\
0x00d8, /* 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume */\
0x0011, /* 4 DSK6713_AIC23_ANAPATH Analog audio path control */\
0x0000, /* 5 DSK6713_AIC23_DIGPATH Digital audio path control */\
0x0000, /* 6 DSK6713_AIC23_POWERDOWN Power down control */\
0x0043, /* 7 DSK6713_AIC23_DIGIF Digital audio interface format */\
0x0081, /* 8 DSK6713_AIC23_SAMPLERATE Sample rate control */\
0x0001 /* 9 DSK6713_AIC23_DIGACT Digital interface activation */ \
};
/*
* main() - Main code routine, initializes BSL and generates tone
*/
void main()
{
DSK6713_AIC23_CodecHandle hCodec;
Uint32 l_input, r_input,l_output, r_output;
/* Initialize the board support library, must be called first */
DSK6713_init();
Expected Outputs
};
/*
* main() - Main code routine, initializes BSL and generates tone
*/
void main()
{
DSK6713_AIC23_CodecHandle hCodec;
DSK6713_AIC23_setFreq(hCodec, 3);
while(1)
{ /* Read a sample to the left channel */
while (!DSK6713_AIC23_read(hCodec, &l_input));
return (temp<<2);
}
Expected Outputs
#include <math.h>
#define PTS 256 //# of points for FFT
#define PI 3.14159265358979
typedef struct {float real,imag;} COMPLEX;
void FFT(COMPLEX *Y, int n); //FFT prototype
float iobuffer[PTS]; //as input and output buffer
float x1[PTS]; //intermediate buffer
short i; //general purpose index variable
short buffercount = 0; //number of new samples in iobuffer
short flag = 0; //set to 1 by ISR when iobuffer full
COMPLEX w[PTS]; //twiddle constants stored in w
COMPLEX samples[PTS]; //primary working buffer
main()
{
for (i = 0 ; i<PTS ; i++) // set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0)); //Re component of twiddle constants
w[i].imag =-sin(2*PI*i/(PTS*2.0)); //Im component of twiddle constants
}
} //end of main
Expected Outputs
67
68
14. To compute Power Density Spectrum (PDS) of a
sequence and signal
#include <math.h>
#define PTS 128 //# of points for FFT
#define PI 3.14159265358979
typedef struct {float real,imag;} COMPLEX;
void FFT(COMPLEX *Y, int n); //FFT prototype
float iobuffer[PTS]; //as input and output buffer
float x1[PTS],x[PTS]; //intermediate buffer
short i; //general purpose index variable
short buffercount = 0; //number of new samples in iobuffer
short flag = 0; //set to 1 by ISR when iobuffer full
float y[128];
COMPLEX w[PTS]; //twiddle constants stored in w
COMPLEX samples[PTS]; //primary working buffer
main()
{
float j,sum=0.0 ;
int n,k,i,a;
for (i = 0 ; i<PTS ; i++) // set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0)); //Re component of twiddle constants
w[i].imag =-sin(2*PI*i/(PTS*2.0)); /*Im component of twiddle constants*/
}
for(n=0;n<PTS;n++)
{
sum=0;
for(k=0;k<PTS-n;k++)
{
sum=sum+(x[k]*x[n+k]); // Auto Correlation R(t)
}
iobuffer[n] = sum;
}
69
for (i = 0 ; i < PTS ; i++)
samples[i].imag = 0.0; //imag components = 0
} //end of main
Additional Programs
Generation of Sinusoid
#include<math.h>
#define FREQ 1000
float a[128];
main()
{
int i=0;
for(i=0;i<128;i++)
{
a[i]=sin(2*3.14*FREQ*i/24000);
printf("%f",a[i]);
}
}
71
Expected Outputs
DIFFERENCE EQUATION
Purpose of Experiment
Sample Code
C Program to Implement Difference Equation
#include <stdio.h>
#include<math.h>
#define FREQ 400
float y[3]={0,0,0};
float x[3]={0,0,0};
float z[128],m[128],n[128],p[128];
main()
72
{
int i=0,j;
float a[3]={ 0.072231,0.144462,0.072231};
float b[3]={ 1.000000,-1.109229,0.398152};
for(i=0;i<128;i++)
{
m[i]=sin(2*3.14*FREQ*i/24000);
}
for(j=0;j<128;j++)
{
x[0]=m[j];
y[0] = (a[0] *x[0]) +(a[1]* x[1] ) +(x[2]*a[2]) - (y[1]*b[1])-(y[2]*b[2]);
z[j]=y[0];
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1] = x[0];
}
}
Expected Outputs
73
Note: To verify the Difference Equation, Observe the output for high frequency and low
frequency by changing variable “FREQ” in the program.
sumim=sumim-x[n]* sin(2*pi*k*n/N);
}
out_real[k]=sumre;
out_imag[k]=sumim;
printf("X([%d])=\t%f\t+\t%fi\n",k,out_real[k],out_imag[k]);
}
}
74