% /home/chuck/Documents/working/12458/matlab/ecgstuff.m
% Sun Nov 13 16:44:18 2022
load ecg_waveforms
Fs=1000;
t1=[0:length(ecg1)-1]/Fs;
t2=[0:length(ecg2)-1]/Fs;

%% Duration, Time Step 
figure;plot(t2,ecg2,'c-',t1,ecg1,'b-');grid on;
xlabel('t,Time, sec');ylabel('ecg');legend('2','1');

%% Bit Depth (Weird)
s=sort(ecg2);
figure;plot(1:length(ecg2),s);grid on;
figure;stem(1:length(ecg2)-1,s(2:end)-s(1:end-1));grid on;

f1=fftaxisshift(fftaxis(t1));
f2=fftaxisshift(fftaxis(t2));
ECG1=fftshift(fft(ecg1));
ECG2=fftshift(fft(ecg2));

%%  Spectrum width and step size
figure;cxplot(f1,ECG1);grid on;
xlabel('f, Frequency, Hz');ylabel('ECG1');

figure;cxplot(f2,ECG2);grid on;
xlabel('f, Frequency, Hz');ylabel('ECG2');

%% Look for peaks
figure;plot(f2,abs(ECG2),'c-+',f1,abs(ECG1),'b-o');grid on;
xlabel('f, Frequency, Hz');ylabel('ECG');legend('2','1');

%% Compare short time vs long and different bit depth
figure;plot(f2,20*log10(abs(ECG2)),'c-+',...
            f1,20*log10(abs(ECG1)),'b-o');grid on;
xlabel('f, Frequency, Hz');ylabel('ECG, dB');legend('2','1');

%% Filter demo
fmax=30;

E2=ECG2.*double(abs(f2)<fmax);
e2=ifft(ifftshift(E2));
figure;plot(t2,ecg2,'c-+',t2,real(e2),'g-x');grid on;
xlabel('t,Time, sec');ylabel('ecg2');
title(['fmax = ',num2str(fmax),' Hz']);
