Please write the code in C language. I want the answer in C Language Task 1: 60
ID: 3283273 • Letter: P
Question
Please write the code in C language.
I want the answer in C Language Task 1: 60 Marks total. Writing a program that generates an accurate output 48 marks. Analysis of the problem-i.e. data requirements 6 marks. Good programming practice-ie proper use of remarks and structuring of the program modules- 6 marks] This task is to simulate signal filtering using a moving average method. A. [10 marks] Generate a sine wave that has a frequency f IHz in [0- 10 seconds] time interval with time steps of 10ms. The generated values should be stored in an array of doubles called S Sli]-sin(2mft) B. [10 marks] Generate random values having the same number of points as the signal in the previous question. The random values should be between -0.1 and 0.1. Store the random s in an array of doubles called N N[irandom values between (-0.1 and 0.1) These random values represent a random noise that could be added to the signal S C. [10 marks] Create an array that contains the sum of the signal in S and noise in N Ali] = S[i] + N[i] D. [10 marks] Save the resulting array on disk as a binary file. E. [5 marks] Open the binary file you created in the previous question and read it. Insert the content of the file in a new array B [15 marks] Generate a filtered version of A using the moving average method. The moving average method operates by averaging a number of points from the input signal to produce each point in the output signal. The equation is shown below: F. i+p/2 Aln] F is the filtered signal, A is the noisy input signal, p is the number of points to use. Suppose that p 10. The equation 10?A[n] For each question in this task, first provide the analysis of the problem by clarifying the inputs, outputs and the data requirements. Then write the program and submit the source code as part of the assignment. It is important to clearly structure your program by using indentation and comments to clarify your ideas.Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
using namespace std;
int main()
{
double T = 10; // total time in seconds
double t = 0; // starting time
double dt = 0.001; // time interval in seconds
int n = T/dt; // number of points
double S[n]; // store points in sine wave
double N[n]; // to store random numbers
double A[n]; // S + N
int f = 1; // frequency in Hz
double B[n]; // to read array from binary file
double F[n] ={0}; // store filtered array
for(int i = 0; i < n; t+= dt,i++)
{
S[i] = sin(2*3.145*f*t); // store sine value
N[i] = ((double) rand() / (float) RAND_MAX) * 0.2 - 0.1; //generate a random number between -0.1 and 0.1
A[i] = S[i] + N[i]; // signal with noise
}
ofstream out("data.bin", ios::binary); // open binary file towrite
if(!out) {
cout << "Cannot open file."; // check for errors
return 1;
}
out.write((char *) &A, sizeof A); // write array in thebinary file
out.close(); // close the binary file
ifstream in("data.bin", ios::binary);// open file to read
if(!in){
cout << "Cannot open file."; // check for errors
return 1;
}
in.read((char *) &B, sizeof B); // read values in arrayB
in.close(); // close the file
int p = 20; // take any value of p ( I have taken 20)
for(int i = 0; i < n; i++)
{
for(int j = i-(p/2); j < i+(p/2);j++)
{
if(j < 0)
continue; // if index is negative skip that addition
F[i] += B[j];
}
F[i] /= p; // store final average value
if( i < 20)
cout << F[i] << " ";
}
return 0;
}