load('data.mat')

b = fir1(32,0.0005,'low');
a = [1 zeros(1, length(b)-1)];

yy = filter(b,a,y);

b = fir1(16,0.1,'high');
a = [1 zeros(1, length(b)-1)];

yyy = filter(b,a,yy);

len = 2000;
stepsize = len;
thres = zeros(size(y));

for m = 1:stepsize:length(y)-len
    data = yyy(m:m+len);
    thres(m:m+len) = mean(data);
end

res = yyy;
res(res >= thres) = 1;
res(res < thres) = -1;

figure(1);
subplot(311);
plot(x,y, 'r', x, yy, 'b');
grid on;
legend('original data', 'lp filtered');
subplot(312);
plot(x,yyy,'r', x, thres);
grid on;
legend('hp filtered', 'threshold');
subplot(313);
plot(x, res, 'r', x, y);
grid on;
legend('filtered', 'original data');
ylim([-1.2 1.2])

