VNRVJIET 22071A04R2
Name of the Laboratory: Digital Signal Processing Experiment No: 4
Name of the Experiment: Fast Fourier Transform Date: 22-11-24
FFT - Fast Fourier Transform
AIM: To find FFT of a given 1-D signal in Code composer studio.
SOFTWARE USED: Code Composer Studio
Code:
#include<stdio.h>
#include<math.h>
int N, k, n, i;
float pi = 3.1416, sumre = 0, sumim = 0;
float out_real[8] = {0.0}, out_imag[8] = {0.0};
int x[32];
void main(void) {
printf(“ 4R2” );
printf("Enter the length of the sequence\n");
scanf("%d", &N);
printf("Enter the sequence\n");
for(i = 0; i < N; i++) {
scanf("%d", &x[i]);
}
for(k = 0; k < N; k++) {
sumre = 0;
sumim = 0;
for(n = 0; n < N; n++) {
sumre = sumre + x[n] * cos(2 * pi * k * n / N);
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]);
}
}
OUTPUT:
4R2
RESULT: Hence, we have found the FFT of a given 1-D signal in Code composer studio.
VNRVJIET 22071A04R2
Name of the Laboratory: Digital Signal Processing Experiment No: 2
Name of the Experiment: Linear Convolution Dat e:22-11-24
Linear Convolution
AIM: To verify Linear Convolution in Code composer studio.
SOFTWARE USED: Code Composer Studio
Code:
#include<stdio.h>
int main() {
int y[7];
int N = 4;
int i, j;
int x[8] = {1, 2, 3, 4, 0, 0, 0, 0};
int h[8] = {4, 3, 2, 1, 0, 0, 0, 0};
for (i = 0; i < 2 * N - 1; i++) {
y[i] = 0;
}
for (i = 0; i < 2 * N - 1; i++) {
for (j = 0; j <= i; j++) {
if (j < N && (i - j) < N) { // Check bounds
y[i] += x[j] * h[i - j];
}
}
}
for (i = 0; i < 2 * N - 1; i++) {
printf("%d ", y[i]);
}
return 0;
}
OUTPUT:
R2
RESULT: Hence, we have verified Linear Convolution in Code composer studio.
VNRVJIET 22071A04R2
Name of the Laboratory: Digital Signal Processing Experiment No: 1
Name of the Experiment: Sine Wave Generation Date: 22-11-24
Sine Wave Generation
AIM: To generate a sine wave in Code composer studio.
SOFTWARE USED: Code Composer Studio
Code:
#include<stdio.h>
#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]);
}
}
OUTPUT:
sample (22071A04R2)
RESULT: Hence, Sine wave is generated in Code composer studio.
VNRVJIET 22071A04R2
Name of the Laboratory: Digital Signal Processing Experiment No: 5
Name of the Experiment: Power Spectrum Density Date: 22-11-24
POWER SPECTRUM DENSITY
AIM: To generate PSD in Code composer studio.
SOFTWARE USED: Code Composer Studio
Code:
#include <stdio.h>
#include <math.h>
#define PTS 128
#define PI 3.141592
typedef struct {
float real;
float imag;
} COMPLEX;
void FFT(COMPLEX *Y, int N);
float iobuffer[PTS];
float x1[PTS], x[PTS];
COMPLEX w[PTS];
COMPLEX samples[PTS];
int main() {
float sum = 0.0;
int n, k, i;
for (i = 0; i < PTS; i++) {
w[i].real = cos(2 * PI * i / (PTS * 2.0));
w[i].imag = -sin(2 * PI * i / (PTS * 2.0));
}
for (i = 0; i < PTS; i++) {
x[i] = sin(2 * PI * 5 * i / PTS); // Example sinusoidal signal
samples[i].real = 0.0;
samples[i].imag = 0.0;
}
for (n = 0; n < PTS; n++) {
sum = 0;
for (k = 0; k < PTS - n; k++) {
sum += x[k] * x[n + k];
}
iobuffer[n] = sum;
}
for (i = 0; i < PTS; i++) {
samples[i].real = iobuffer[i];
samples[i].imag = 0.0;
}
FFT(samples, PTS);
for (i = 0; i < PTS; i++) {
x1[i] = sqrt(samples[i].real * samples[i].real + samples[i].imag * samples[i].imag);
}
return 0;
}
void FFT(COMPLEX *Y, int N) {
COMPLEX temp1, temp2;
int i, j, k;
int upper_leg, lower_leg;
int leg_diff;
int num_stages = 0;
int index, step;
i = 1;
do {
num_stages += 1;
i *= 2;
} while (i != N);
leg_diff = N / 2;
step = (PTS * 2) / N;
for (i = 0; i < num_stages; i++) {
index = 0;
for (j = 0; j < leg_diff; j++) {
for (upper_leg = j; upper_leg < N; upper_leg += (2 * leg_diff)) {
lower_leg = upper_leg + leg_diff;
// Compute butterfly operations
temp1.real = Y[upper_leg].real + Y[lower_leg].real;
temp1.imag = Y[upper_leg].imag + Y[lower_leg].imag;
temp2.real = Y[upper_leg].real - Y[lower_leg].real;
temp2.imag = Y[upper_leg].imag - Y[lower_leg].imag;
Y[lower_leg].real = temp2.real * w[index].real - temp2.imag * w[index].imag;
Y[lower_leg].imag = temp2.real * w[index].imag + temp2.imag * w[index].real;
Y[upper_leg].real = temp1.real;
Y[upper_leg].imag = temp1.imag;
}
index += step;
}
leg_diff /= 2;
step *= 2;
}
j = 0;
for (i = 1; i < (N - 1); i++) {
k = N / 2;
while (k <= j) {
j -= k;
k /= 2;
}
j += k;
if (i < j) {
temp1.real = Y[j].real;
temp1.imag = Y[j].imag;
Y[j].real = Y[i].real;
Y[j].imag = Y[i].imag;
Y[i].real = temp1.real;
Y[i].imag = temp1.imag;
}
}
return;
}
OUTPUT:
(22071A04R2)
RESULT: Hence, we have generated PSD in Code composer studio.
VNRVJIET 22071A04R2
Name of the Laboratory: Digital Signal Processing Experiment No: 3
Name of the Experiment: Circular Convolution Dat e:22-11-24
Circular Convolution
AIM: To verify Circular Convolution in Code composer studio.
SOFTWARE USED: Code Composer Studio
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(“ 4R2” );
printf("Enter the length of the first sequence\n");
scanf("%d", &m);
printf("Enter the length of the second sequence\n");
scanf("%d", &n);
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];
}
}
printf("the circular convolution is\n");
for(i = 0; i < n; i++) {
printf("%d \t", y[i]);
}
}
OUTPUT:
4R2
(22071A04R2)
RESULT: Hence, we have verified circular convolution in Code composer studio.