0% found this document useful (0 votes)
10 views21 pages

FIR Filter Design and Implementation Guide

The document outlines the design and implementation of Finite Impulse Response (FIR) and Infinite Impulse Response (IIR) filters using MATLAB and Code Composer Studio (CCS) on a TMS320C6748 Processor Kit. It details the steps for designing FIR filters using MATLAB's functions, implementing them in CCS, and verifying the results against MATLAB outputs, as well as the process for designing and implementing a Butterworth IIR filter. The document includes code examples for both FIR and IIR filters, along with procedures for testing and real-time tuning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

FIR Filter Design and Implementation Guide

The document outlines the design and implementation of Finite Impulse Response (FIR) and Infinite Impulse Response (IIR) filters using MATLAB and Code Composer Studio (CCS) on a TMS320C6748 Processor Kit. It details the steps for designing FIR filters using MATLAB's functions, implementing them in CCS, and verifying the results against MATLAB outputs, as well as the process for designing and implementing a Butterworth IIR filter. The document includes code examples for both FIR and IIR filters, along with procedures for testing and real-time tuning.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EXPERIMENT 05

5.A)

Aim: To design a Finite Impulse Response (FIR) on DSP processor by interfacing CCS
and MATLAB

Software used: Code Composer Studio and


MATLAB

Components used: TMS320C6748 Processor Kit


Theory:

A digital filter processes discrete signals, modifying their

frequency content according to design specifications. FIR filters are commonly used in
digital signal processing due to their stability and linear phase response. FIR filters
operate using a finite number of past input samples and have an impulse response that
settles to zero within a finite time.

An FIR filter of order NNN is defined by the following equation:


Steps for FIR Filter Design

1. Filter Design: Using MATLAB, design the filter and obtain the coefficients
using functions like fir1 or fir2.
2. DSP Implementation: Using CCS, implement the filter using the designed
coefficients.
3. Verification: Test the DSP-implemented filter against MATLAB’s software-
based filter to verify the results.

TYPE OF FILTER USED: -

WINDOW FILTER IN MATLAB: -

A windowed FIR filter in MATLAB can be designed easily using the Filter Designer
tool, which allows users to create FIR filters by applying window functions to an ideal
sinc response. Windowed FIR filters are commonly used due to their simplicity,
stability, and ability to provide a linear phase response, which is essential for preserving
waveform shape in applications like audio and communication.

The ideal low-pass filter has an infinite impulse response in the form of a sinc
function, which cannot be realized practically. To approximate this ideal response, we
use a finite number of coefficients and apply a window function to truncate and shape
the sinc function, reducing undesirable ringing effects and controlling the trade-off
between the sharpness of the transition and sidelobe levels in the frequency response.
C Program for FIR filter:

# include <stdio.h>
# include <math.h>
float x[100];
float XR[100];
float Xl[100];
int N = 100;
float b[51];
float y[100];
float amplitude[100];
float phase[100];
float h[51] = {
-0.00844987477671691, 0.00735255301795108, 0.0116847006547848, -
0.00425566137542113, -0.0143106865490118, 1.68362687178645e-17,
0.0159427113982737, 0.00528433484843854, -0.0161885801865472, -
0.0113837144368529, 0.0146521079621851, 0.0180123355682603, -
0.0109141828962539, -0.0248298426174695, 0.00446543430823437,
0.0314639528545312, 0.00547907291127654, -0.0375363565072118, -
0.0205077541048356, 0.0426898397130334, 0.0448226152022344, -
0.0466144319801682, -0.0942793094002802, 0.0490704261125797,
0.313771708465521, 0.44915720362693, 0.313771708465521, 0.0490704261125797, -
0.0942793094002802, -0.0466144319801682, 0.0448226152022344,
0.0426898397130334, -0.0205077541048356, -0.0375363565072118,
0.00547907291127654, 0.0314639528545312, 0.00446543430823437, -
0.0248298426174695, -0.0109141828962539, 0.0180123355682603,
0.0146521079621851, -0.0113837144368529, -0.0161885801865472,
0.00528433484843854, 0.0159427113982737, 1.68362687178645e-17, -
0.0143106865490118, -0.00425566137542113, 0.0116847006547848,
0.00735255301795108, -0.00844987477671691
};

int main(void) {
int i, j, n;
for(i = 0; i < 100; i++)
{
x[i]=0;
x[i] = 4 + sin(2 * 3.14 * 30 * i / 100) + 3 * cos(2 * 3.14 * 2 * i / 100);
}

for(n = 0; n < N; n++) {


y[n] = 0;
for(i = 0; i < 5; i++) {
if(n >= i) {
y[n] = y[n] + (h[i] * x[n - i]);
}
}
}

for(j = 0; j < N; j++) {


XR[j] = 0;
Xl[j] = 0;
for(i = 0; i < N; i++) {
XR[j] = XR[j] + Xl[j] * cos(2 * 3.14 * j * i / N);
Xl[j] = Xl[j] - y[i] * sin(2 * 3.14 * j * i / N);
}
}

printf("\nAmplitude\tPhase\n");

for(i = 0; i < N; i++) {


amplitude[i] = sqrt((XR[i] * XR[i]) + (Xl[i] * Xl[i]) / N);
phase[i] = atan(Xl[i] / XR[i]);
printf("%f\t%f ", amplitude[i], phase[i]);
printf("\n");
}

return 0;
}

Input:
This input signal is created with:
x[i] = 4 + sin (2 * 3.14 * 30 * i / 100) + 3 * cos (2 * 3.14 * 2 * i / 100);
The impulse signal h[] is created using matlab:
float h[51] = {
-0.00844987477671691, 0.00735255301795108, 0.0116847006547848, -
0.00425566137542113, -0.0143106865490118, 1.68362687178645e-17,
0.0159427113982737, 0.00528433484843854, -0.0161885801865472, -
0.0113837144368529, 0.0146521079621851, 0.0180123355682603, -
0.0109141828962539, -0.0248298426174695, 0.00446543430823437,
0.0314639528545312, 0.00547907291127654, -0.0375363565072118, -
0.0205077541048356, 0.0426898397130334, 0.0448226152022344, -
0.0466144319801682, -0.0942793094002802, 0.0490704261125797,
0.313771708465521, 0.44915720362693, 0.313771708465521, 0.0490704261125797, -
0.0942793094002802, -0.0466144319801682, 0.0448226152022344,
0.0426898397130334, -0.0205077541048356, -0.0375363565072118,
0.00547907291127654, 0.0314639528545312, 0.00446543430823437, -
0.0248298426174695, -0.0109141828962539, 0.0180123355682603,
0.0146521079621851, -0.0113837144368529, -0.0161885801865472,
0.00528433484843854, 0.0159427113982737, 1.68362687178645e-17, -
0.0143106865490118, -0.00425566137542113, 0.0116847006547848,
0.00735255301795108, -0.00844987477671691
}

Output:
Procedure:
Part A: FIR Filter Design in MATLAB
1. Open MATLAB and define the filter specifications:
o Sampling frequency (fs), Cutoff frequency (fcutoff), and Filter order.
2. Design the FIR Filter:
o Use the fir1 function to design a low-pass FIR filter.

3. Visualize the Frequency Response:


o Plot the frequency response to check the filter design using:
4. Save Filter Coefficients:
o Export the coefficients to a file that can be accessed by CCS:
Part B: FIR Filter Implementation in Code Composer Studio
1. Open CCS and Create a New Project:
o Select the appropriate DSP target (e.g., TMS320C2000, TMS320C6000).
2. Load the Coefficients:
o Load the [Link] file generated by MATLAB into the CCS
project. These coefficients will be used for the filter implementation.
3. Define Global Variables:
o Define arrays for storing coefficients, input data, and output data.
4. Implement the FIR Filter Algorithm:
o In the CCS project, write a C function to perform the FIR filtering using
the loaded coefficients:
5. Initialize Variables:
o Set up variables such as the circular buffer for incoming samples, the
index pointer for the buffer, and output variables.
6. Build and Load Program to DSP:
o Compile the project, and upload the code to the DSP processor.
Part C: Testing and Verification
1. Generate Test Signals in MATLAB:
o Create test signals, such as sinusoidal signals, and store them in a format
readable by CCS.
2. Load Test Signal into DSP:
o Load the test signal into the DSP processor to apply the FIR filter.
3. Capture Filtered Output:
o Store the DSP-processed output, then transfer it back to MATLAB or use
a debugger in CCS to examine the output.
4. Compare with MATLAB Output:
o Apply the same FIR filter to the test signal in MATLAB and compare the
results to validate DSP implementation:
Part D: Real-Time Tuning (Optional)
1. Interface MATLAB with CCS (using Embedded Coder or Simulink) to modify
parameters in real-time and monitor DSP performance.
Screenshots of output

RESULT OUTPUT
INPUT GRAPH
AMPLITUDE GRAPH
PHASE GRAPH

RESULT:-
The results of FIR filter have been verified and obtained.
5.B)
Aim: To design an Infinite Impulse Response (IIR) on DSP processor by interfacing CCS
and MATLAB

Software used: Code Composer Studio and


MATLAB

Components used: TMS320C6748 Processor Kit

Theory:
TYPE OF FILTER USED: -

BUTTERWORTH FILTER IN MATLAB: -

The Butterworth filter is a type of Infinite Impulse Response (IIR) digital filter known
for its maximally flat frequency response in the passband, meaning it provides a
smooth, continuous output without ripples. This characteristic makes it an excellent
choice for applications where a flat frequency response is critical.

In MATLAB, the Butterworth filter can be designed using the Filter Designer
function, which computes the filter coefficients based on the desired filter order and
cutoff frequency. The function returns the numerator b and denominator a coefficients
of the digital filter's transfer function.
C Program for IIR filter:

# include <stdio.h>
# include <math.h>

#define N 100 // Length of input signal

float x[N]; // Input signal


float y[N]; // Output signal
float amplitude[N]; // Amplitude spectrum
float phase[N]; // Phase spectrum

float b[] = {-0.303454544013818, -0.286261055483893, -0.270997511791944, -


0.257408359227879, -0.245280384145027, -0.234434699935740, -
0.224720456578179, -0.216009864284244, -0.208194228549811, -
0.201180770180625, -0.194890059431201, -0.189253934264377, -
0.184213803077536, -0.179719254958999, -0.175726917692022, -
0.172199516781942, -0.169105098801774, -0.166416390101216, -
0.164110267966901, -0.162167326074563, -0.160571519847297, -
0.159309880362682, -0.158372287913428, -0.157751298358917, -
0.157442017116372};
float a[] = {0.939818977459842,
0.829910406317903,
0.732338917272804,
0.645470895187195,
0.567943381798552,
0.498612852835524,
0.436515006590058,
0.380832952819522,
0.330871866990875,
0.286038662444919,
0.245825588249239,
0.209796921769805,
0.177578118927698,
0.148846930328170,
0.123326101104572,
0.100777355793409,
0.0809964336074211,
0.0638089890128611,
0.0490672111461897,
0.0366470459873283,
0.0264459293289472,
0.0183809579411528,
0.0123874420684673,
0.00841779539021599,
0.00644072951495858}; // Example feedback coefficients
int order_b = sizeof(b) / sizeof(b[0]);
int order_a = sizeof(a) / sizeof(a[0]); // Number of feedback coefficients

int main(void) {
int i, j, n;

// Initialize signal x with a sum of sine and cosine components


for(i = 0; i < N; i++) {
x[i] = 4 + sin(2 * M_PI * 30 * i / N) + 3 * cos(2 * M_PI * 2 * i / N);
}

// Apply the IIR filter


for(n = 0; n < N; n++) {
y[n] = 0;

for(i = 0; i < order_b; i++) {


if (n >= i) {
y[n] += b[i] * x[n - i]; // Use past inputs
}
}

// Feedback part (IIR)


for(j = 1; j < order_a; j++) { // j starts from 1 because a[0] is assumed to be 1
if (n >= j) {
y[n] -= a[j] * y[n - j]; // Use past outputs
}
}
}

// Optional: Perform DFT (Discrete Fourier Transform) to compute the amplitude and
phase spectra
float XR[N], Xl[N];
for(j = 0; j < N; j++) {
XR[j] = 0;
Xl[j] = 0;
for(i = 0; i < N; i++) {
XR[j] += y[i] * cos(2 * M_PI * j * i / N);
Xl[j] += -y[i] * sin(2 * M_PI * j * i / N);
}
}

// Calculate amplitude and phase


printf("\nAmplitude\tPhase\n");
for(i = 0; i < N; i++) {
amplitude[i] = sqrt((XR[i] * XR[i]) + (Xl[i] * Xl[i])) / N; // Normalized by N
phase[i] = atan2(Xl[i], XR[i]); // Use atan2 for proper phase calculation
printf("%f\t%f\n", amplitude[i], phase[i]);
}

return 0;
}

INPUT:
The input signal x is defined within the code as a combination of a constant value, a
sine wave, and a cosine wave. Specifically, the input signal is calculated as:

Output:
Procedure:
Step 1: Filter Design in MATLAB
1. Specify Filter Requirements: Define the filter type (e.g., low-pass, high-pass,
band-pass), cutoff frequencies, and order based on your application.
2. Design the Filter:
o Use MATLAB’s Filter Designer function to create the IIR filter. For
instance:
We used IIR filter design “BUTTERWORTH” for different filter responses.
3. Quantize Coefficients:
o DSPs often require coefficients to be represented in fixed-point format
for efficient processing. Quantize the coefficients if necessary using
MATLAB’s Fixed-Point Toolbox.
4. Generate C Header Files:
o Export the filter coefficients to a header file that will be used by the DSP
code. This can be done manually or using MATLAB functions like printf
.
Step 2: Setting Up CCS for DSP
1. Configure the DSP Environment:
o Open Code Composer Studio (CCS) and set up your DSP project.
Configure the DSP platform, clock rates, and other initialization
parameters.
2. Include Header Files:
o Add the filter coefficients header file generated from MATLAB to your
CCS project. Ensure that your code can access these coefficients during
the execution.
Step 3: Implement the Filter in CCS
1. Write the Filter Code:
o Implement the IIR filter using Direct Form II Transposed or other
efficient filter structures, as IIR filters often suffer from stability issues.
o A typical IIR implementation in C might look like this:
c
2. Test the Filter:
o Simulate the filter using sample input signals to verify the
implementation. CCS allows for real-time debugging, which can help
identify issues with the filter's performance or stability.
3. Optimize for Real-Time Processing:
o Ensure that the implementation is optimized for real-time constraints by
minimizing the filter order or using DSP-specific instructions and
libraries.
Step 4: Testing and Verification
1. Simulate the Complete System:
o Test the implemented filter in CCS with test data to validate that the DSP
produces the correct output.
2. Compare with MATLAB Output:
o Run the same test signal through MATLAB’s filter function and compare
the output to verify that the DSP-based filter performs correctly.
Step 5: Deployment
1. Deploy and Test on the DSP Hardware:
o Once satisfied with the simulation results, deploy the code to the DSP
hardware, and run additional tests in a real-time environment to confirm
the filter’s stability and performance.
This workflow integrates MATLAB’s powerful filter design capabilities with CCS’s
real-time DSP processing, making it a robust approach to digital filter design for DSP
applications.
Screenshots of output

RESULT OUTPUT
AMPLITUDE GRAPH
PHASE GRAPH

RESULT:-
The results of IIR filter have been verified and obtained.

You might also like