Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 1 60 Marks total. Writing a program that generates an accurate output- 48 m

ID: 3719345 • Letter: T

Question

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 frequencyf lHz in [0 10 seconds] time interval with time steps of 10ms. The generated values should be stored in an array of doubles called S S[i]-sin(2nft) 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 randorm values in an array of doubles called N N[i]random 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 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] ni-p/2 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 becomes: Fli] 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

For task A we will need the value of frequency f and time t as input, the output will be the value of sine function for those values.

For task B we need the value of minimum and the maximum value of interval in which we need to generate random variables of noise (N)

For task C we need values of A[i] and N[i] to calculate A[i] ( signal with noise).

For task D we need N( array of signal with noise) to write into a binary file.

For task E we need the data file which is in binary and an array to write the data into.

For task F we need value of signal with noise(B) and p(number of points around which we want to find average) to calculate filtered signal.

Throughout the code appropriate comments are provided.

---- Source Code in C++ ----

#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 to write

if(!out) {

cout << "Cannot open file."; // check for errors

return 1;

}

out.write((char *) &A, sizeof A); // write array in the binary 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 array B

  

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote