clc;
clear;

fc = 50; % filter cutoff frequency
fs = 10000; % sample frequency
signal_length = 1; % signal length in seconds

pulse_length = 0.01; % pulse length in seconds
single_pulse = [ones(1,fs*pulse_length) 0.3*ones(1,fs*pulse_length) 0.6*ones(1,fs*pulse_length)];

t = 0:(1/fs):(signal_length-1/fs);
x = [repmat(single_pulse, 1, 3) zeros(1, round(fs*(signal_length - pulse_length*9)))];

N = length(x);
X = fft(x,N) / fs;
w = 0:(fs/N):(fs*(1-1/N));

figure
subplot(2,2,1);
plot(t,x);
ylabel('Signal x(t)');
xlabel('t');

subplot(2,2,2);
plot(w,abs(X));
ylabel('Spektrum X(f)');
xlabel('f');

W = sum(x) / fs
W_cum = cumsum(x) / fs;

subplot(2,2,3);
plot(t,W_cum);
ylabel('W_{cum}(t)');
xlabel('t');


%%
[b,a] = butter(1, fc/(fs/2));
x_filtered = filter(b,a,x);

X_filtered = fft(x_filtered,N) / fs;

figure
subplot(2,2,1);
plot(t,x_filtered);
ylabel('Signal x_{filtered}(t)');
xlabel('t');

subplot(2,2,2);
plot(w,abs(X_filtered));
ylabel('Spektrum X_{filtered}(f)');
xlabel('f');

W_filtered = sum(x_filtered) / fs
W_cum_filtered = cumsum(x_filtered) / fs;

subplot(2,2,3);
plot(t,W_cum_filtered);
ylabel('W_{cum\_filtered}(t)');
xlabel('t');