Let's review the slides about the polynomials:








To summarize the two functions, polyval and polyfit:
1. Given a series of X values and the coefficients (an, an-1, ..., a0), polyval is a function to calculate the 'Y' values in:

It is a very simple function, as the example followed:

p=[1.1 -19 0 62]; % the coefficient vector

44.1000000000000    -5.20000000000000    -79.3000000000000    -171.600000000000    -275.500000000000    -384.400000000000    -491.700000000000    -590.800000000000    -675.100000000000    -738    -772.900000000000    -773.200000000000    -732.300000000000    -643.600000000000    -500.500000000000    -296.400000000000    -24.6999999999992    321.200000000000    747.900000000001    1262

Definitely, you can do the following to get the same Y values:
y=1.1*x.^3 + (-19)*x.^2 + 0*x.^1 + 62*x.^0; % Don't forget the element-wise operation for the exponential values.

44.1000000000000    -5.20000000000000    -79.3000000000000    -171.600000000000    -275.500000000000    -384.400000000000    -491.700000000000    -590.800000000000    -675.100000000000    -738    -772.900000000000    -773.200000000000    -732.300000000000    -643.600000000000    -500.500000000000    -296.400000000000    -24.6999999999998    321.200000000001    747.900000000001    1262

The two results are the same.

2. polyfit() is a function that can create a series of 'coefficients' of a polynomial which allows you to adjust the 'degree' of the polynomial to fit the a continuous curve to the discrete data you collected.

For example, you sampled the X and the Y data as:
x=[0.9 1.5 3 4 6 8 9.5];
y=[0.9 1.5 2.5 5.1 4.5 4.9 6.3];
hold on
p=polyfit(x,y,5); % the polynomial has a degree of 5
xx=linspace(0,10,30); % create more data points in your fitting curve
y2=polyval(p,xx); % get the corresponding 'Y' values for the fitting curve

The fitted curve looks like:

You can fit more points among these points to predict a continuous curve. The function to 'fit' more points inside is polyfit().
Change the 'degree' will change the fitted curve dramatically.

1. Load the ECG signal in from Matlab:
load noisyecg.mat

Then treat all the points in the data as your collected data points, create a corresponding 'X' series for these Y values.

2, Use polyfit(x,y,degree) to get the coefficients of the fitted curves. Use different degree values: 1, 10, 20, 30, 40, 50, 60 to plot 7 different fitted curves. Have these figures in your report and describe the differences.

3. Use the original ECG signal subtract the pionts in each of the fitted curve, then plot the results (7 different results). Have these figures in your report and describe the differences. Discuss in your report about your findings.

4. The 1-3 tasks are called 'De-trend' the ECG signal, which removes the noisy offset of the signal. Now, let's detrend one more ECG signal using your script (try to not copy the code but just type it from scratch). Use the peak detection method to count how many heart beats are in this signal. The data can be found here:
ECG signal.

Submit an electronic version of your report to the email. Again, have figures, captions, title, explanations, and your code as the appendix. Due Monday.