%% Line: Multiple Observations

clear y

x=1:.5:20;

k=10;

[m n]=size(x);

%Obtains noisy data
for jnd=1:k
    for ind=1:n
        y(ind,jnd)=x(ind)+normrnd(0,.2);
    end
end

% Computes the average at each x value
yavg=mean(y,2);

% One observation of the original noisy data
figure(1)
plot(x,y(:,1))

% A second observation of the noisy data
figure(2)
plot(x,y(:,2))

% The averaged data
figure(3)
plot(x,yavg)


%% Sin Example: Multiple Observations 
clear y

x=1:.5:20;

k=10;

[m n]=size(x);

for jnd=1:k
    for ind=1:n
        y(ind,jnd)=.1*x(ind)+sin(x(ind))+normrnd(0,.2);
    end
end

yavg=mean(y,2);

figure(4)
plot(x,y(:,1))

figure(5)
plot(x,y(:,2))

figure(6)
plot(x,yavg)

figure(7)
plot(x,.1*x+sin(x))


%% Sin Example: Linear and Median Filters

clear y

x=1:.1:20;

[m n]=size(x);

for ind=1:n
    y(ind)=.1*x(ind)+sin(x(ind))+normrnd(0,.05);
end

figure(8)
plot(x,y)



ylin(1)=y(1);
ymed(1)=y(1);

for ind=2:(n-1)
    ylin(ind)=(ylin(ind-1)+y(ind)+y(ind+1))/3;
    ymed(ind)=median([ymed(ind-1) y(ind) y(ind+1)]);
end

ylin(n)=(y(n)+ylin(n-1))/2;
ymed(n)=median([ymed(n-1) y(n)]);

% The Linear Filter
figure(9)
plot(x,ylin)

% The Median Filter
figure(10)
plot(x,ymed)







