Question: When analyzing a time sequence of measurements made on a noisy system,
ID: 3622214 • Letter: Q
Question
Question: When analyzing a time sequence of measurements made on anoisy system, it is often useful to smooth the data so that trends are easier
to spot. One simple smoothing technique is a so-called unweighted
moving average. Suppose a data set consists of n values. These data can
be smoothed by taking a moving average of m points, where m is some
number significantly less than n. The average is unweighted because old
values count just as much as newer values. The formula for calculating
the smoothed average value Si corresponding to the ith value in the data
set, is
Given a set of n points, an algorithm for calculating a moving average of m values is:
1. Calculate the sum s of the first m points. The first average (Sm) equals s/m.
2. For each value of i = m + 1 to n, add the ith value to s and subtract the (i – m)th value. Then calculate the average for this new sum.
3. Repeat step 2 until i = n.
Write a PHP application that reads a file of numerical values and creates a new file containing the original values and the moving average smoothed values. You may wish to create an HTML interface that specifies the file name and the number of points to be included in the moving average.
Here is my code
<?php
$m=$_POST["m"];
$filename=$_POST["filename"];
$inFile ="$filename";// "problem16data.txt";
$outFile="smooth.csv";
$in = fopen($inFile, "r") or die("Can't open file");
$out=fopen($outFile, "w");
$line=fgets($in);
while ((!feof($in)) && ($found == 1))
{ $line=fgets($in);
fscanf($in,"%s %f",$numbers);
$n= strlen($in); // total number of elements input data file
$S= 0;
for ($idx=0; $idx < $m; ++$idx)
{
$S= $idx + $S; // finding sum
}
$Sm= $S/$m; // finding average
$S1=0;
while ($i= $m+1<=$n)
{
$S1=$i+$S-($i-$m);
$Sm1=$S1/$m; } // finding average
echo "$inFile" ;
echo "$Sm1 <br>";
fprintf($out,"%s %f ", $inFile. "<br>" .$Sm1);}
fclose($in);
fclose($out);
?>
<html>
<head>
</head>
<body>
<form method="post" action="php16.php">
Specify the file name: <input type="text" name="filename" value="problem16data.txt" /><br />
Specify the number of points between to be included in the moving average: <input type="text" name="m" value="5" /> <br />
<input type="button" value="Click here" />
</form>
</body>
</html>
And the data file I created is just a txt file with 12 four digit numbers.
Explanation / Answer
Dear, Helping you with 1 partRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.