0% found this document useful (0 votes)
18 views

Lab1 - C PROGRAM

The document contains C code examples that generate and print common signal processing functions including continuous and discrete sine waves, continuous and discrete cosine waves, a unit impulse function, a ramp function, a square wave, and a parabolic function. Each code example defines parameters, generates the function values in a loop, and prints the results.

Uploaded by

Ankita Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Lab1 - C PROGRAM

The document contains C code examples that generate and print common signal processing functions including continuous and discrete sine waves, continuous and discrete cosine waves, a unit impulse function, a ramp function, a square wave, and a parabolic function. Each code example defines parameters, generates the function values in a loop, and prints the results.

Uploaded by

Ankita Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C PROGRAM

1. Continuous Sine wave


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

int main() {
// Define parameters for the sine wave
double amplitude = 1.0; // Amplitude of the sine wave
double frequency = 1.0; // Frequency of the sine wave (in Hz)
double sampling_rate = 44100.0; // Sampling rate (samples per second)
double duration = 5.0; // Duration of the sine wave (in seconds)

// Calculate the number of samples


int num_samples = (int)(duration * sampling_rate);

// Generate and print the sine wave


for (int i = 0; i < num_samples; i++) {
// Calculate the time for the current sample
double time = i / sampling_rate;

// Calculate the sine value for the current time


double sine_value = amplitude * sin(2.0 * M_PI * frequency * time);
// Print the sine value (you can output it to a file or a sound device)
printf("%lf\n", sine_value);
}

return 0;
}

2. Discrete sine wave


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

int main() {
// Define parameters for the sine wave
double amplitude = 1.0; // Amplitude of the sine wave
double frequency = 1.0; // Frequency of the sine wave (in Hz)
int num_samples = 100; // Number of samples in the discrete sine wave

// Generate and print the discrete sine wave


for (int i = 0; i < num_samples; i++) {
// Calculate the current angle in radians
double angle = 2.0 * M_PI * frequency * i / num_samples;

// Calculate the sine value for the current angle


double sine_value = amplitude * sin(angle);

// Print the sine value (you can output it to a file or process it further)
printf("%lf\n", sine_value);
}

return 0;
}

3. Continuous cos wave


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

int main() {
// Define parameters for the cosine wave
double amplitude = 1.0; // Amplitude of the cosine wave
double frequency = 1.0; // Frequency of the cosine wave (in Hz)
double sampling_rate = 44100.0; // Sampling rate (samples per second)
double duration = 5.0; // Duration of the cosine wave (in seconds)

// Calculate the number of samples


int num_samples = (int)(duration * sampling_rate);

// Generate and print the cosine wave


for (int i = 0; i < num_samples; i++) {
// Calculate the time for the current sample
double time = i / sampling_rate;

// Calculate the cosine value for the current time


double cosine_value = amplitude * cos(2.0 * M_PI * frequency * time);

// Print the cosine value (you can output it to a file or a sound device)
printf("%lf\n", cosine_value);
}

return 0;
}

4. Discrete cos wave


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

int main() {
// Define parameters for the cosine wave
double amplitude = 1.0; // Amplitude of the cosine wave
double frequency = 1.0; // Frequency of the cosine wave (in Hz)
int num_samples = 100; // Number of samples in the discrete cosine wave
// Generate and print the discrete cosine wave
for (int i = 0; i < num_samples; i++) {
// Calculate the current angle in radians
double angle = 2.0 * M_PI * frequency * i / num_samples;

// Calculate the cosine value for the current angle


double cosine_value = amplitude * cos(angle);

// Print the cosine value (you can output it to a file or process it further)
printf("%lf\n", cosine_value);
}

return 0;
}

5. Unit impulse
#include <stdio.h>

int main() {
// Define the length of the impulse function
int length = 10; // Change this to the desired length

// Create an array to store the impulse function


int impulse[length];
// Initialize all values in the array to zero
for (int i = 0; i < length; i++) {
impulse[i] = 0;
}

// Set the value at the desired index (usually index 0) to 1


impulse[0] = 1;

// Print the impulse function


printf("Unit Impulse Function:\n");
for (int i = 0; i < length; i++) {
printf("%d ", impulse[i]);
}
printf("\n");

return 0;
}

6. Ramp
#include <stdio.h>

int main() {
// Define the length of the ramp function
int length = 10; // Change this to the desired length
// Create an array to store the ramp function
int ramp[length];

// Initialize the array with the ramp values


for (int i = 0; i < length; i++) {
ramp[i] = i;
}

// Print the ramp function


printf("Ramp Function:\n");
for (int i = 0; i < length; i++) {
printf("%d ", ramp[i]);
}
printf("\n");

return 0;
}

7. Square Wave
#include <stdio.h>

int main() {
// Define parameters for the square wave
int period = 10; // Number of samples per period
int num_periods = 5; // Number of periods
int length = period * num_periods; // Total length of the wave

// Create an array to store the square wave


int square_wave[length];

// Initialize the array with the square wave values


for (int i = 0; i < length; i++) {
if ((i / period) % 2 == 0) {
square_wave[i] = 1; // Positive level
} else {
square_wave[i] = -1; // Negative level
}
}

// Print the square wave


printf("Square Wave:\n");
for (int i = 0; i < length; i++) {
printf("%d ", square_wave[i]);
}
printf("\n");

return 0;
}
8. Parabola
#include <stdio.h>

int main() {
// Define parameters for the parabolic function
double a = 1.0; // Coefficient for x^2
double b = 2.0; // Coefficient for x
double c = 1.0; // Constant term
int num_points = 10; // Number of points on the parabola
double x, y;

// Calculate and print the points on the parabola


printf("Parabolic Function (y = %.1fx^2 + %.1fx + %.1f):\n", a, b, c);
for (int i = 0; i < num_points; i++) {
x = i; // You can adjust the range of x values as needed
y = a * x * x + b * x + c;
printf("x = %.1f, y = %.1f\n", x, y);
}

return 0;
}

You might also like