{"id":51727,"date":"2009-02-05T19:45:00","date_gmt":"2009-02-05T19:45:00","guid":{"rendered":"\/2009\/02\/23\/ece231-spring-2009-programming-assignment-1"},"modified":"2009-08-30T00:17:36","modified_gmt":"2009-08-30T00:17:36","slug":"ece231-spring-2009-programming-assignment-1","status":"publish","type":"post","link":"http:\/\/pullmonkey.com\/2009\/02\/05\/ece231-spring-2009-programming-assignment-1\/","title":{"rendered":"ECE231 – Spring 2009 – Programming Assignment 1"},"content":{"rendered":"

This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.<\/a><\/p>\n

Part A<\/h1>\n

1. Create a structure (called point) that includes a time and y value (both doubles).<\/b><\/p>\n

This is very simple using the typedef struct<\/b> and listing your variables.<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>typedef<\/span> struct<\/span>\n<\/tt>{\n<\/tt>  double<\/span> time;\n<\/tt>  double<\/span> y;\n<\/tt>} point;\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

2. Create a main program that has an array of 40 points (or structures), keeps track of the number of values in the array and has variables for each of the values needed to calculate above.<\/b><\/p>\n

In the main program we will need an array of points p<\/i><\/b>, an integer with the total number of points values<\/i><\/b>, and 4 double variables, one for each of the functions (I used vert_shift<\/i><\/b>, amp<\/i><\/b>, freq<\/i><\/b>, and phase_shift<\/i><\/b>).<\/p>\n

Then you will need to call each function and for testing I printed each value out to make sure everything was coming in right. It is short, simple, and to the point like all programming should be. Your main should look something like this:<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt>24\n<\/tt>25<\/strong>\n<\/tt>26\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>int<\/span> main()\n<\/tt>{\n<\/tt>  point p[40<\/span>];          \/\/ array of points<\/span>\n<\/tt>  int<\/span> values = 0<\/span>;       \/\/ total number of values in the points array<\/span>\n<\/tt>  double<\/span> vert_shift=0<\/span>;  \/\/ vertical shift<\/span>\n<\/tt>  double<\/span> amp=0<\/span>;         \/\/ amplitude<\/span>\n<\/tt>  double<\/span> freq=0<\/span>;        \/\/ frequency<\/span>\n<\/tt>  double<\/span> phase_shift=0<\/span>; \/\/ phase shift<\/span>\n<\/tt>\n<\/tt>  values = read_data(p);\n<\/tt>  cout << "<\/span>Values: <\/span>"<\/span><\/span> << endl;\n<\/tt>  for<\/span>(lcv=0<\/span>; lcv < values; lcv++)\n<\/tt>    cout << "<\/span>  <\/span>"<\/span><\/span> <<  p[lcv].time << "<\/span>  <\/span>"<\/span><\/span> << p[lcv].y << endl;\n<\/tt>  cout << "<\/span>n<\/span>Total Number of values = <\/span>"<\/span><\/span> << values << endl;\n<\/tt>  vert_shift = vertical(p, values);\n<\/tt>  cout << "<\/span>n<\/span>Vertical shift = <\/span>"<\/span><\/span> << vert_shift << endl;\n<\/tt>  amp = amplitude(p, values);\n<\/tt>  cout << "<\/span>n<\/span>Amplitude = <\/span>"<\/span><\/span> << amp << endl;\n<\/tt>  freq = frequency(p, values);\n<\/tt>  cout << "<\/span>n<\/span>Frequency = <\/span>"<\/span><\/span> << freq << endl;\n<\/tt>  phase_shift = pshift(p, values);\n<\/tt>  cout << "<\/span>n<\/span>Phase Shift = <\/span>"<\/span><\/span> << phase_shift << endl;\n<\/tt>  return<\/span> 0<\/span>;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

3. The main program should then call the read_data function. This function will have one argument (the array of points - or structures - which will be passed as a pointer to the function) and will return the number of values in the array. It should read the data from the data file using a pointer and the arrow (->) operator.<\/b><\/p>\n

First, we are going to be reading from a file in this function so we are going to need to include the fstream<\/b> library.<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>#inlcude<\/span> <fstream> \/\/ reading from file<\/span>\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Next, we need to create the prototype for this function called read_data<\/b>. This will go above the main. The read_data<\/b> function will need the array of points and will return the total number of points as an integer. So it should look like this:<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>int<\/span> read_data(point *);\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Now below the main we can create our function and lets call the array of points a<\/i><\/b>. In this function we will need 2 more variables, an integer (total<\/i><\/b>) for counting the number of points that we read from the file and an ifstream variable (f<\/i><\/b>) for opening and closing the file.<\/p>\n

First, we need to try to open the file using f.open(\"sp09prog1.txt\")<\/b>. Next, we need to test to make sure the file was opened and that we can read from it and if not then print out error message and exit. If the file was opened then we need to read in the values into the point array and count the number of points using the total<\/i><\/b> integer variable. We do this until we read the end of the file and we find out if we are at the end of the file with f.eof()<\/b>. Finally, we will want to return the total<\/i><\/b>. It should look like this:<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>int<\/span> read_data(point *a)\n<\/tt>{\n<\/tt>  ifstream f;  \/\/ file streaming variable<\/span>\n<\/tt>  int<\/span> total=0<\/span>; \/\/ counts the total number of points in file<\/span>\n<\/tt>\n<\/tt>  f.open("<\/span>sp09prog1.txt<\/span>"<\/span><\/span>);\n<\/tt>\n<\/tt>  if<\/span>(f.fail())\n<\/tt>  {\n<\/tt>    cout << "<\/span>ERROR: File could not be opened<\/span>"<\/span><\/span> << endl;\n<\/tt>    exit(1<\/span>);\n<\/tt>  }\n<\/tt>\n<\/tt>  while<\/span>(!f.eof())\n<\/tt>  {\n<\/tt>    f >> a->time >> a->y;\n<\/tt>    total++;\n<\/tt>    a++;\n<\/tt>  }\n<\/tt>  return<\/span> total - 1<\/span>;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

Notice that we return total - 1<\/b>. This is because we increment total<\/i><\/b> and then we find out it is the end of the file. So we incremented one too many times so we need to subtract 1 before returning it.<\/p>\n

4. Main should then call the vertical function to calculate the vertical shift. This function has two arguments (the array of points - passed as a pointer - and the number of values in the array). It will calculate the vertical shift by finding the average of the maximum and minimum y-values. You will need to search through the array of points to find these max and min values.<\/b>
\n5. Next, main will call the amplitude function to calculate the amplitude. This function has two arguments (the array of points - passed as a pointer - and the number of values in the array; you must also use the arrow operator in this function) and will return the amplitude. To find the amplitude, you need to calculate half the difference between the maximum and minimum values of the y-values (you will need to search through the array for the maximum and minimum values again).<\/b><\/p>\n

The functions are both almost identical. The only difference is the equation at the end so we will do them at the same time.<\/p>\n

First, we will create the prototypes. Both functions take the array of points and the integer total number of points and they both return a double. So the prototypes should look like this:<\/p>\n\n\n
\n
1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt><\/pre>\n<\/td>\n
\n
\n<\/tt>double<\/span> vertical(point *, int<\/span> );\n<\/tt>double<\/span> amplitude(point *, int<\/span> );\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

So lets call the point array p<\/i><\/b> and the integer total<\/i><\/b>.<\/p>\n

To find the max and min values we need to create 2 double variables (max<\/i><\/b> and min<\/i><\/b>) and an integer (lcv<\/i><\/b>) that we will use for the loop.<\/p>\n

Next we will need to set both max<\/i><\/b> and min<\/i><\/b> to the first y<\/i><\/b> element in the array of points. Then we will have a loop and start from 0 to total<\/i><\/b>. Each time we run through this loop we need to do 3 things:<\/p>\n

    \n
  • 1. Test if max<\/i><\/b> is less than the current y<\/i><\/b> value? If yes then set max<\/i><\/b> to the current y<\/i><\/b><\/li>\n
  • 2. Test if min<\/i><\/b> is greater than the current y<\/i><\/b> value? If yes then set min<\/i><\/b> to the current y<\/i><\/b><\/li>\n
  • 3. Increment a<\/i><\/b> to the next position in the array. Because we are using a pointer to the array we can do this easily by doing a++<\/b><\/li>\n<\/ul>\n

    Next we will just return the result of the equation as a double. So your functions should look something like this:<\/p>\n\n\n
    \n
    1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt>24\n<\/tt>25<\/strong>\n<\/tt>26\n<\/tt>27\n<\/tt>28\n<\/tt>29\n<\/tt>30<\/strong>\n<\/tt>31\n<\/tt>32\n<\/tt>33\n<\/tt>34\n<\/tt><\/pre>\n<\/td>\n
    \n
    \n<\/tt>double<\/span> vertical(point *a, int<\/span> total)\n<\/tt>{   \n<\/tt>  double<\/span> min; \/\/ minimum y value found in array<\/span>\n<\/tt>  double<\/span> max; \/\/ maximum y value found in array<\/span>\n<\/tt>  int<\/span> lcv;    \/\/ loop control variable<\/span>\n<\/tt>  max = min = a->y;\n<\/tt>  for<\/span>(lcv=0<\/span>; lcv < total; lcv++) \n<\/tt>  {\n<\/tt>    if<\/span>(max < a->y)\n<\/tt>      max = a->y;\n<\/tt>    if<\/span>(min > a->y)\n<\/tt>      min = a->y;\n<\/tt>    a++;\n<\/tt>  }\n<\/tt>  return<\/span> (max + min) \/ 2<\/span>.0<\/span>;\n<\/tt>}\n<\/tt>double<\/span> amplitude(point *a, int<\/span> total)\n<\/tt>{\n<\/tt>  double<\/span> min; \/\/ minimum y value found in array<\/span>\n<\/tt>  double<\/span> max; \/\/ maximum y value found in array<\/span>\n<\/tt>  int<\/span> lcv;    \/\/ loop control variable<\/span>\n<\/tt>  max = min = a->y;\n<\/tt>  for<\/span>(lcv=0<\/span>; lcv < total; lcv++)\n<\/tt>  {\n<\/tt>    if<\/span>(max < a->y)\n<\/tt>      max = a->y;\n<\/tt>    if<\/span>(min > a->y)\n<\/tt>      min = a->y;\n<\/tt>    a++;\n<\/tt>  }\n<\/tt>  return<\/span> (max - min) \/ 2<\/span>.0<\/span>;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

    6. The next function called by main is the frequency function that has two arguments (the array of points - passed as a pointer - and the number of values in the array; again using the arrow operator to access values) and will return the frequency. To calculate the frequency, find the period which is time difference between the maximum and minimum y-values and multiply by 2. Then, divide to calculate the frequency. Please note that you are using the difference between time values, not the y values, to perform the overall computation.<\/b>
    \n7. Finally, main will call the pshift function that contains two arguments (the array of points - passed as a pointer - and the number of values in the array; again using the arrow operator to access values) and will return the phase shift or horizontal shift. This shift can be found by finding the average of the time values where the maximum and minimum y-values occur; then subtracting it from half the period (see step 6 to find the period).<\/b><\/p>\n

    The functions for steps 6 and 7 are both almost identical expect for the equation at the end so we will do these both at the same time.<\/p>\n

    For the frequency function we will need to use the absolute value function in the cmath library. To include this you will need to do this at the top of your file.<\/p>\n\n\n
    \n
    1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n
    \n
    \n<\/tt>#include<\/span> <cmath><\/span>\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

    Like the functions in step 4 and 5 they need to have the array of points and the integer total number of points and they both return a double. So the prototypes should look like this:<\/p>\n\n\n
    \n
    1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt><\/pre>\n<\/td>\n
    \n
    \n<\/tt>double<\/span> frequency(point *, int<\/span> );\n<\/tt>double<\/span> pshift(point *, int<\/span> );\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

    So lets call the point array p<\/i><\/b> and the integer total<\/i><\/b>.<\/p>\n

    To find the max and min values we need to create 2 pointers to point variables (max<\/i><\/b> and min<\/i><\/b>) and an integer (lcv<\/i><\/b>) that we will use for the loop.<\/p>\n

    Next we will need to set both max<\/i><\/b> and min<\/i><\/b> pointers to the same address as a<\/i><\/b> points which is the first position in the array of points. Then we will have a loop and start from 0 to total<\/i><\/b>. Each time we run through this loop we need to do 3 things:<\/p>\n

      \n
    • 1. Test if max->y<\/i><\/b> is less than the current a->y<\/i><\/b> value? If yes then set max<\/i><\/b> to the current a<\/i><\/b> address.<\/li>\n
    • 2. Test if min->y<\/i><\/b> is greater than the current a->y<\/i><\/b> value? If yes then set min<\/i><\/b> to the current a<\/i><\/b> address.<\/li>\n
    • 3. Increment a<\/i><\/b> to the next position in the array. Because we are using a pointer to the array we can do this easily by doing a++<\/b><\/li>\n<\/ul>\n

      Next we will just return the result of the equation as a double. So your functions should look something like this:<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt>24\n<\/tt>25<\/strong>\n<\/tt>26\n<\/tt>27\n<\/tt>28\n<\/tt>29\n<\/tt>30<\/strong>\n<\/tt>31\n<\/tt>32\n<\/tt>33\n<\/tt>34\n<\/tt>35<\/strong>\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>double<\/span> frequency(point *a, int<\/span> total)\n<\/tt>{\n<\/tt>  point *min; \/\/ minimum y value location found in array<\/span>\n<\/tt>  point *max; \/\/ maximum y value location found in array<\/span>\n<\/tt>  int<\/span> lcv;    \/\/ loop control variable<\/span>\n<\/tt>  max = min = a;\n<\/tt>  for<\/span>(lcv=0<\/span>; lcv < total; lcv++)\n<\/tt>  {\n<\/tt>    if<\/span>(max->y < a->y)\n<\/tt>      max = a;\n<\/tt>    if<\/span>(min->y > a->y)\n<\/tt>      min = a;\n<\/tt>    a++;\n<\/tt>  } \n<\/tt>  return<\/span> abs(PI \/ (max->time - min->time));\n<\/tt>} \n<\/tt>double<\/span> pshift(point *a, int<\/span> total)\n<\/tt>{\n<\/tt>  point *min; \/\/ minimum y value location found in array<\/span>\n<\/tt>  point *max; \/\/ maximum y value location found in array<\/span>\n<\/tt>  int<\/span> lcv;    \/\/ loop control variable<\/span>\n<\/tt>  max = min = a;\n<\/tt>  for<\/span>(lcv=0<\/span>; lcv < total; lcv++)\n<\/tt>  {\n<\/tt>    if<\/span>(max->y < a->y)\n<\/tt>      max = a;\n<\/tt>    if<\/span>(min->y > a->y)\n<\/tt>      min = a;\n<\/tt>    a++;\n<\/tt>  }\n<\/tt>  a -= total;\n<\/tt>  return<\/span> (2<\/span> * PI \/ frequency(a, total)) \/ 2<\/span> - (max->time + min->time) \/ 2<\/span>.0<\/span>;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Notice how in the pshift function after the loop we do a -= total<\/b>. This is because a<\/i><\/b> is currently pointing the last element in the array and if we want to call frequency and pass the address of the first element in the array of points then we need to set a<\/i><\/b> back to the first element.<\/p>\n

      Also notice that the frequency function uses a variable called PI<\/i><\/b>. This is the value of pi so at the top above the main you will need to define it as a constant. You can do this one of 2 ways: <\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>#define<\/span> PI 3<\/span>.14159<\/span>\n<\/tt>or \n<\/tt>const<\/span> double<\/span> PI=3<\/span>.14159<\/span>;\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Either way will work but I prefer to use the #define<\/b> since it uses less memory and only takes more processing when you compile the program not in running it.<\/p>\n

      Outcome<\/h2>\n

      Using the text file provided this is what I got when I ran the program:<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt>24\n<\/tt>25<\/strong>\n<\/tt>26\n<\/tt>27\n<\/tt>28\n<\/tt>29\n<\/tt>30<\/strong>\n<\/tt>31\n<\/tt>32\n<\/tt>33\n<\/tt>34\n<\/tt>35<\/strong>\n<\/tt>36\n<\/tt>37\n<\/tt>38\n<\/tt>39\n<\/tt>40<\/strong>\n<\/tt>41\n<\/tt>42\n<\/tt>43\n<\/tt>44\n<\/tt>45<\/strong>\n<\/tt>46\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>[scnaegle@<\/span>egor ece231]$<\/span> .\/a.out \n<\/tt>Values:<\/span> \n<\/tt>  1<\/span>.5708<\/span>  -4<\/span>\n<\/tt>  1<\/span>.6791<\/span>  -3<\/span>.3551<\/span>\n<\/tt>  1<\/span>.7875<\/span>  -2<\/span>.7403<\/span>\n<\/tt>  1<\/span>.8958<\/span>  -2<\/span>.1845<\/span>\n<\/tt>  2<\/span>.0041<\/span>  -1<\/span>.7135<\/span>\n<\/tt>  2<\/span>.1125<\/span>  -1<\/span>.3495<\/span>\n<\/tt>  2<\/span>.2208<\/span>  -1<\/span>.1094<\/span>\n<\/tt>  2<\/span>.3291<\/span>  -1<\/span>.0044<\/span>\n<\/tt>  2<\/span>.4374<\/span>  -1<\/span>.0395<\/span>\n<\/tt>  2<\/span>.5458<\/span>  -1<\/span>.2131<\/span>\n<\/tt>  2<\/span>.6541<\/span>  -1<\/span>.5169<\/span>\n<\/tt>  2<\/span>.7624<\/span>  -1<\/span>.9369<\/span>\n<\/tt>  2<\/span>.8708<\/span>  -2<\/span>.4533<\/span>\n<\/tt>  2<\/span>.9791<\/span>  -3<\/span>.0421<\/span>\n<\/tt>  3<\/span>.0874<\/span>  -3<\/span>.6756<\/span>\n<\/tt>  3<\/span>.1958<\/span>  -4<\/span>.3244<\/span>\n<\/tt>  3<\/span>.3041<\/span>  -4<\/span>.9579<\/span>\n<\/tt>  3<\/span>.4124<\/span>  -5<\/span>.5467<\/span>\n<\/tt>  3<\/span>.5208<\/span>  -6<\/span>.0631<\/span>\n<\/tt>  3<\/span>.6291<\/span>  -6<\/span>.4831<\/span>\n<\/tt>  3<\/span>.7374<\/span>  -6<\/span>.7869<\/span>\n<\/tt>\n<\/tt>\n<\/tt>  3<\/span>.8457<\/span>  -6<\/span>.9605<\/span>\n<\/tt>  3<\/span>.9541<\/span>  -6<\/span>.9956<\/span>\n<\/tt>  4<\/span>.0624<\/span>  -6<\/span>.8906<\/span>\n<\/tt>  4<\/span>.1707<\/span>  -6<\/span>.6505<\/span>\n<\/tt>  4<\/span>.2791<\/span>  -6<\/span>.2865<\/span>\n<\/tt>  4<\/span>.3874<\/span>  -5<\/span>.8155<\/span>\n<\/tt>  4<\/span>.4957<\/span>  -5<\/span>.2597<\/span>\n<\/tt>  4<\/span>.6041<\/span>  -4<\/span>.6449<\/span>\n<\/tt>  4<\/span>.7124<\/span>  -4<\/span>\n<\/tt>\n<\/tt>Total Number of values = 30<\/span>\n<\/tt>\n<\/tt>Vertical shift = -4<\/span>\n<\/tt>\n<\/tt>Amplitude = 2<\/span>.9956<\/span>\n<\/tt>\n<\/tt>Frequency = 1<\/span>.93329<\/span>\n<\/tt>\n<\/tt>Phase Shift = -1<\/span>.5166<\/span>\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      <\/p>\n

      Part B<\/h1>\n

      Using MATLAB, you need to plot the original data using the data file sp09prog1.txt. To do this, load the file into MATLAB, then assign the first column to be the x values and the second column to be the y values. You can then use the plot command to plot these (you should only plot the points - not any connecting lines).<\/b><\/p>\n

      This is easy. Use the load()<\/b> function to read in data from the file into a matrix.<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>table = load('sp09prog1.txt')\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Then we need to get the data into arrays of time<\/i><\/b> and y<\/i><\/b>. To do this use matrixvairable<\/i>(row<\/i>,column<\/i>)<\/b>. Use a colon (:<\/b>) to get everything in either the row or column. In this case we will want all rows in the first column for time<\/i><\/b> and all rows and second column for y<\/i><\/b>.<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>time = table(: , 1)\n<\/tt>y = table(: , 2)\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Then plot the data using the plot()<\/b> function. For this you will need to pass 3 things (x values, y values, and symbol). Our x values is the array time<\/i><\/b> and our y values is array y<\/i><\/b>.<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>plot(time, y, 'o')\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Then, using the values you found in the C++ program for each of the four variables, create a cosine curve that can be plotted over the data. You will need to create a matrix of x-values that run from 1 to 2\u03c0 with an increment of \u03c0\/100. Then, create a matrix of y values using the cosine equation: where vs is the vertical shift, ps is the phase shift, amp is the amplitude and freq is the frequency. Plot these x and y values using a red line.<\/b><\/p>\n

      This is just the same as the first part only we need to find the x<\/i><\/b> and y<\/i><\/b> values using equations. But first lets set all the values we will need for our calculations which we can get from Part A<\/b> of the program.<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>vs = -4\n<\/tt>amp = 2.9956\n<\/tt>freq = 1.93329\n<\/tt>ps = -1.5166\n<\/tt>PI = 3.14159\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Next we will have to create the array x<\/i><\/b> with all the x values. In this case we are going to go from 0 to 2pi and step by pi\/100. To do this we have a colon (:<\/b>) between each argument like this:<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>x = 0 : PI \/ 100 : 2 * PI\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Now, to create the array y<\/i><\/b> with all the y values we use the equation we were given.<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>y = vs + amp * (cos(freq * x - ps))\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Now, all we need to do is plot it on the graph. However, we want to plot the new equation on the same plot as the last one. To do this we need to use the command hold on<\/b>. We also want the new graph to look different so lets make it a full line 4px wide and the color be dark red. Like this:<\/p>\n\n\n
      \n
      1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt><\/pre>\n<\/td>\n
      \n
      \n<\/tt>hold on\n<\/tt>plot(x, y, '', 'LineWidth', 4, 'Color', [.6 0 0])\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n

      Now run it and see if your two line match up at all.<\/p>\n","protected":false},"excerpt":{"rendered":"

      This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here. Part A 1. Create a structure (called point) that includes a time and y value (both doubles). […]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,6,3,14,30],"tags":[33,34],"_links":{"self":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts\/51727"}],"collection":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/comments?post=51727"}],"version-history":[{"count":2,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts\/51727\/revisions"}],"predecessor-version":[{"id":57501,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/posts\/51727\/revisions\/57501"}],"wp:attachment":[{"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/media?parent=51727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/categories?post=51727"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pullmonkey.com\/wp-json\/wp\/v2\/tags?post=51727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}