Question

In: Computer Science

Write C code that contains a function called triangle_generator that outputs a triangle wave at a...

Write C code that contains a function called triangle_generator that outputs a triangle wave at a specified frequency (in hertz) using a specified sample rate (in hertz), and for a specified time duration (in seconds). These parameters are float type. The output you generate are floating point numbers between -1 and 1, one number per output line. The math trig functions in C use radians while all the specifications here are in hertz (cycles per second). One hertz is 2*Pi radians.

Solutions

Expert Solution

The required answer is given below in case of any doubts you can ask me in comments.


Explanation:

main.c

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


// prototype
float triangle_generator(float frequency, float sample_rate, float timeDuration);


// driver function.
int main()
{
    // variable declaration
    float frequency, sampleRate, timeDuration;
    // To take inputs from the user
    printf("Enter Frequency, Sample Rate and Time separated by space:\n"); 
    scanf("%f%f%f",&frequency, &sampleRate, &timeDuration);
    // getting value.
    float res = triangle_generator(frequency, sampleRate, timeDuration); 
    // Displaying value.
    printf("Result = %f", res);
    return 0;
}


// Function for finding the sin value
float triangle_generator(float freq, float sampleRate, float timeDuration) 
{
    float temp = sampleRate/freq; // cycles per second
    float x = sin((2*3.1414)/temp*timeDuration); 
    return x;
}

Output:


Related Solutions

In C++, 
 Define a structure Triangle that contains three Point members. Write a function that computes...
In C++, 
 Define a structure Triangle that contains three Point members. Write a function that computes the perimeter() of a Triangle. Write a program that reads the coordinates of the points, calls your function, and displays the result. C++
Written in C code: Using a structure called person, write a C function called compare_age that...
Written in C code: Using a structure called person, write a C function called compare_age that takes two person structure pointers as input and returns 1 if the first person is older, -1 if the second person is older, or 0 if they have exactly the same birthday. Hint: Compare the years first, then if equal compare months, if they are equal compare days. The structure called person should contain the following; has fields for name (50 char array), dateOfBirth...
Part 2 Write a C code to create a user defined function called RecArea that will...
Part 2 Write a C code to create a user defined function called RecArea that will calculate the area of a rectangle. Call the function to print the area of the rectangle on the screen. Note: The user will enter the length and width of a rectangle (decimal values, float) and you Part 3 (Continue on part 2) Create another user defined function called TriArea that will calculate the area of a triangle. Print the area of the rectangle on...
Code in C Write a program whose inputs are three integers, and whose outputs are the...
Code in C Write a program whose inputs are three integers, and whose outputs are the largest of the three values and the smallest of the three values. Ex: If the input is: 7 15 3 the output is: largest: 15 smallest: 3 Your program must define and call the following two functions. The LargestNumber function should return the largest number of the three input values. The SmallestNumber function should return the smallest number of the three input values. int...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Write a small section of Python code that defines a function called check_even(value) - the function...
Write a small section of Python code that defines a function called check_even(value) - the function takes a numerical value as input and returns True or False based on whether the provided argument is divisible by 2 (i.e. is it odd or is it even). If the argument provided is not a number (as determined by built-in the isdigit() function - which only works on string data) then rather than crashing, the function should raise an exception which is caught...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT