I need help to find Heart rate in 2 ECGs signals that I'm analyzing.
This is what I did since now:
Normalization and mean value removing
50Hz filtering band stop
filtering with 0.5 Hz Butterworth high pass
R-peaks filtering 15Hz and 20Hz Butterworth hight and low pass
Now, I'm basing my analysis on this paper but I'm stuck on Heart rate detection algorithms with Autocorrelation of energy signal / Thresholding of energy signal. I can't understand how to get the heart rate, can anyone tell me (with a code example please). Here is what I'm doing:
valvular_heart_disease = load('disease.mat');
figure;plot(valvular_heart_disease.val);
healthy_control = load('healthy.mat');
figure;plot(healthy_control.ecg);
fs = 1000; % Sampling frequency - Hz
n_dis = length(valvular_heart_disease.val); % length
n_heal = length(healthy_control.ecg);
timevec_dis = (0:n_dis-1)/fs; % time vector
timevec_heal = (0:n_heal-1)/fs; % time vector
% Detrend data vhd
detrend_data_vhd = detrend(valvular_heart_disease.val);
% Normalize data vhd
max_value_vhd = max(detrend_data_vhd);
normalized_vhd = detrend_data_vhd/max_value_vhd;
% Detrend data hc
detrend_data_hc = detrend(healthy_control.ecg);
% Normalize data hc
max_value_hc = max(detrend_data_hc);
normalized_hc = detrend_data_hc/max_value_hc;
%figure;plot(normalized_vhd);
%%%%%%%%%%% FILTERING %%%%%%%%%%%
% Notch def band stop
wo = 50/(fs/2);
bw = wo/35;
[b,a] = iirnotch(wo,bw);
% Butter High Pass
[z,p] = butter(2,0.5/(fs/2),'high');
% Butter low-high
%fcutlow=15; %low cut frequency in Hz
%fcuthigh=20; %high cut frequency in Hz
%[c,d]=butter(4,[fcutlow,fcuthigh]/(fs/2),'bandpass');
[c,d] =butter(2,15/(fs/2),'low');
[h,i] =butter(2,20/(fs/2),'high');
% Notch
ecg_vhd_notch = filter(b,a,normalized_vhd);
ecg_hc_notch = filter(b,a,normalized_hc);
% Butter
ecg_vhd_btr = filter(z,p,ecg_vhd_notch);
ecg_hc_btr = filter(z,p,ecg_hc_notch);
% Butter low-high
%l
ecg_vhd_low = filter(c,d,ecg_vhd_btr);
ecg_hc_low = filter(c,d,ecg_hc_btr);
%h
ecg_vhd_high = filter(h,i,ecg_vhd_low);
ecg_hc_high = filter(h,i,ecg_hc_low);
% Plotting Notch
figure
subplot(2,1,1)
plot(timevec_dis, normalized_vhd, 'b', timevec_dis, ecg_vhd_notch, 'r');
title('Segnale VHD Notch')
xlabel('sec')
ylabel('amp')
figure
subplot(2,1,1)
plot(timevec_heal, normalized_hc, 'b', timevec_heal, ecg_hc_notch, 'r');
title('Segnale HC Notch')
% Plotting after butter
figure
subplot(2,1,1)
plot(timevec_dis, normalized_vhd, 'b', timevec_dis, ecg_vhd_btr, 'r');
title('Segnale VHD Butter')
figure
subplot(2,1,1)
plot(timevec_heal, normalized_hc, 'b', timevec_heal, ecg_hc_btr, 'r');
title('Segnale HC Butter')
xlabel('time')
ylabel('amplitude')
% filtering with high pass and low pass Butterworth filters
% with cut-off frequencies 15 Hz and 20 Hz
figure
subplot(2,1,1)
plot(timevec_dis, normalized_vhd, 'b', timevec_dis, ecg_vhd_high, 'r');
title('Segnale VHD Butter')
figure
subplot(2,1,1)
plot(timevec_heal, normalized_hc, 'b', timevec_heal, ecg_hc_high, 'r');
title('Segnale HC Butter')
% Heart rate detection
et = abs(ecg_hc_high).^2;
plot(timevec_heal, et)
ac = xcorr(et);
plot(ac)
th = 2*mean(et);
[pks, locs] = findpeaks(j);
% what now?
Rwave = pcks>th;
% what now?
I suggest searching for wavelet-based methods. They are the most commonly used for this task
This paper might help. (The main author was a lecturer of mine at the Research Institute where I studied my Master's Degree)
Related
I am trying to generate ultrashort pulses and then seeing the resulting frequency comb using a fourier transform, I have used the gaussian pulse and pulse train functions to try and do this but it is not coming out correctly - I am hoping to be able to change the variables at the top to see the changes quickly
If here is a solution or any good resources that could help me I would appreciate it alot... Thanks
Code is here:
fs = 1e17 ; % sample rate
frep = 7.5e9; % repition rate
f_sig = 1.93e15; %frequency of signal
tc = gauspuls('cutoff',f_sig,100,[],-80);
t = -tc*200:1/fs:tc*200;
[x1,x2,x3] = gauspuls(t,f_sig,0.5);
figure(1);
plot(t,x1,t,x3)
xlabel('Time (s)')
ylabel('Waveform')
ts = 0:1/fs/2:tc*50000000 ;
d = 0:1/frep:tc*50000000 ; %delay
y = pulstran(ts,d,x,fs);
figure(2);
plot(ts,y)
%Frequency Comb FFT
fsamp = fs;
L= length(t); %signal length
NFFT = 2^nextpow2(L);
FFT = abs(fftshift(fft(x3,NFFT))/NFFT); %FFT with FFTshift for both negative & positive frequencies
f = fsamp*(-NFFT/2:(NFFT/2-1))/NFFT; %Frequency Vector
figure(3)
plot(f/1e9,10*log10(FFT/1e-3));
title('Magnitude of FFT');
xlabel('Frequency (GHz)');
ylabel('Magnitude |X(f)|');
%xlim([-100 100])
I'm trying to better understand upsampling and downsampling, and am using an audio signal to do so. I start with a base sampling rate of 66.15kHz, upsample by 2, run it through an LPF (sample rate of 66.15kHz, cutoff frequency of half that), then downsample by 2.
To visualize the differences between the initial and final signal, I created two spectrograms, with different sampling frequencies. There's lots of clear similarities, as well as a tighter band around the signals in the lower frequency version (as a result of the upsampling?), but I think the best thing to do is just take some sort of subtraction of the images. The problem is that the matrices are different sizes, so I can't use approaches found here or here.
Should I just upsample both of them to be the same size? Will the differences still be there?
The spectrograms in question:
Here's the code I used, some parts are commented out that may help to visualize things along the way:
%clc, clear all, close all;
NFFT = 1:10:4000; % used like F in the spectrogram fxn
WINDOW = 2000;
NOVERLAP = 0;
preupsample = audioread('audioclip.wav');
Fo = 66150;
orig_nyq = Fo/2;
U = 2;
D = 3;
postupsample = upsample(preupsample, U);
[preLPF, ~, W] = myfft(postupsample,U*Fo,length(postupsample));
% figure, plot(W,mag2db(abs(preLPF))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
% title('Magnitude Response after upsampling');
filterorder = 100;
Fpass = orig_nyq;
Fs = U*Fo;
Rpass = 0.00057565; % .1dB passband ripple
Rstop = 1e-4; % -80dB stopband attenuation
LPF = firceqrip(filterorder,Fpass/(Fs/2),[Rpass Rstop],'passedge');
% fvtool(LPF,'Fs',Fs,'Color','White');
filtered_upsample = filter(LPF,1,postupsample);
[postLPF, ~, W] = myfft(filtered_upsample,U*Fo,length(filtered_upsample));
% figure, plot(W,mag2db(abs(postLPF))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
% title('Magnitude Response after applying LPF');
postdownsample = downsample(filtered_upsample,D);
[postDS, w, W] = myfft(postdownsample,U*Fo/D,length(postdownsample));
% figure, plot(W,mag2db(abs(postDS))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
% title('Magnitude Response after downsampling');
%[~,F1,T1,P1]=
figure, spectrogram(preupsample(:,1),WINDOW,NOVERLAP,NFFT,Fo,'yaxis');
%[~,F2,T2,P2]=
figure, spectrogram(postdownsample(:,1),WINDOW,NOVERLAP,NFFT,Fo*U/D,'yaxis');
% surf(T2,F2,10*(log10(abs(P2))-log10(abs(P1))),'edgecolor','none');
% view(90,-90);
% axis tight;
The contents of myfft.m:
function [X, w, W] = myfft(x, F, N)
X = fftshift(fft(x, N));
w = fftshift((0:N-1)/N*2*pi);
w(1:N/2) = w(1:N/2) - 2*pi;
W = F*w;
end
I want to study the response of the system.
I want to find the resonant frequency of the sprung mass (m1) and the resonant frequency of the unsprung mass (m2).
Because I am not sure if I have understood the meaning of resonant. Let's say that we have a suspension system and we stimulated with a step response 0.1(m).
That means, for example at 1(Hz) the sprung mass (m1) vibrating at the high level? and at 10(Hz) the unsprung mass (m2) vibrating at the high level?
I've read about bode plot and I did some things as you can see below but I don't know if this is the way to find the Resonant Frequencies. Maybe FFT is what I'm looking for?
In my bode plot (Picture below):
Blue Line: The first peak corresponds to the resonant frequency of the sprung mass (m1)? and Orange Line: Τhe second peak (which is higher than the first peak of orange line) corresponds to the resonant frequency of the unsprung mass (m2)?
If yes how can I get these values? What I need to add to my code?
Can I change the units from decibel to m?
clc;
clear all;
close all;
%% Parameters
m1 = 400; % Sprung Mass (kg)
m2 = 40; % Unsprung Mass (kg)
k1 = 21000; % Suspension Stiffness (N/m)
k2 = 150000; % Tire Stiffness (N/m)
b1 = 1500; % Suspension Damping (N*s/m)
b2 = 0; % Tire Damping Coefficient (N*s/m)
%% Transfer Function
num1 = [(0) (0) (b1*b2) (b1*k2+b2*k1) (k1*k2)];
den1 = [(m1*m2) (m1*b1+m1*b2+m2*b1) (m1*k1+m1*k2+m2*k1+b1*b2) (b1*k2+k1*b2) (k1*k2)];
G1 = tf(num1,den1); % G1(s) = X1(s)/Xr(s)
num2 = [(0) (m1*b2) (m1*k2+b1*b2) (b1*k2+b2*k1) (k1*k2)];
den2 = [(m1*m2) (m1*b1+m1*b2+m2*b1) (m1*k1+m1*k2+m2*k1+b1*b2) (b1*k2+k1*b2) (k1*k2)];
G2 = tf(num2,den2); % G2(s) = X2(s)/Xr(s)
%% Results
figure;
step(0.1*G1,0:0.01:5);
hold on;
step(0.1*G2,0:0.01:5);
title('');
legend('m1','m2');
xlabel('Time (s)');
ylabel('Amplitude (m)');
axis([]);
grid on;
figure;
bode(G1);
hold on;
bode(G2);
title('');
legend('m1','m2');
axis([]);
grid on;
Reaponse, Bode Plot and 2 DOF System
I studies on Digital Signal Filtering and I have a file includes some signal data. I wrote Matlab Program to get 23 to 27 Hz from original signal. but filtered signal is different. my desire filter is bandpass but my code work like low pass filter.
this is my code :
clc;
clear all;
load('SignalFile.mat');
%number of sample
SampleNumber = 60000;
%create hamming window
hamm = hamming((SampleNumber))'; %'
% there is 4 data in SignalFile.mat
for pl=1:4
% get name of data in signalFile.mat
data = eval(['mydata' num2str(pl)]);
[n,c] = size(data);
nk=SampleNumber;
% there is 2 signal in each data. but main signal exists on data(1:nk,1);
% nk is Sample Number.
mydata = data(1:nk,1) ;
encodedata = data(1:nk,2);
% Sample Rate my this file equal to data length / 4 ->> ~ 39000 sample
% per second.
fs = floor(n/4); % Sampling rate [Hz]
noSamples = nk; % Number of samples
f = 0 : fs/noSamples : fs - fs/noSamples; % Frequency vector
figure;
subplot(2,2,1);
plot(mydata);
x_fft = abs(fft(mydata));
subplot(2,2,2);
plot(f,x_fft);
xlim([1 100]);
centerf = 25; % 25 Hz Center
bw=2; %Bandwisth
fc=pi*centerf; %Center Frequency
L = nk; %sample number;
%Compute Filter
hsuup=(-(L-1)/2:(L-1)/2);
hideal1=hamm.*(2*(fc+bw)*(sin(2*(fc+bw)*hsuup/fs)./(2*(2*fc+bw)*hsuup/fs)));
hideal2=hamm.*(2*(fc-bw)*(sin(2*(fc-bw)*hsuup/fs)./(2*(2*fc+bw)*hsuup/fs)));
h_bpf=(hideal1-hideal2);
% transform mydata to Ferequency Domain
comp_sig_fft=fft(mydata)'; %'
% transform Filter Windows to Ferequency Domain
h_bpf_fft=fft(h_bpf);
% Filter Signal
s_fft=comp_sig_fft.*h_bpf_fft;
%
band_passed_signal=real(ifft(s_fft));
subplot(2,2,3);
plot(band_passed_signal);
x_fft = abs(fft(band_passed_signal));
subplot(2,2,4);
plot(f,x_fft);
xlim([1 100]);
end
is there any idea?
best regards.
my datafile uploaded here :
http://wikisend.com/download/574638/SignalFile.mat
result Images :
https://www.imageupload.co.uk/image/ZLzM
if you look at image, you can find under 20hz signal exists in filtered signal yet.
I'm new to Matlab for LTI signal processing and wondering if anyone can help with something that I'm sure is meant to be basic. I've spent hours and hours researching and obtaining background information and still cannot obtain a clear path to tackle these problems. So far, from scratch, I have generated a signal required and managed to use the fft function to produce the signal's DFT:
function x = fourier_rikki(A,t,O)
Fs = 1000;
t = 0:(1/Fs):1;
A = [0.5,0,0.5];
N = (length(A) - 1)/2;
x = zeros(size(t));
f1 = 85;
O1 = 2*pi*f1;
for k = 1:length(A)
x1 = x + A(k)*exp(1i*O1*t*(k-N-1));
end
f2 = 150;
O2 = 2*pi*f2;
for k = 1:length(A);
x2 = x + A(k)*exp(1i*O2*t*(k-N-1));
end
f3 = 330;
O3 = 2*pi*f3;
for k = 1:length(A);
x3 = x + A(k)*exp(1i*O3*t*(k-N-1));
end
signal = x1 + x2 + x3;
figure(1);
subplot(3,1,1);
plot(t, signal);
title('Signal x(t) in the Time Domain');
xlabel('Time (Seconds)');
ylabel('x(t)');
X = fft(signal); %DFT of the signal
subplot(3,1,2);
plot(t, X);
title('Power Spectrum of Discrete Fourier Transform of x(t)');
xlabel('Time (Seconds)');
ylabel('Power');
f = linspace(0, 1000, length(X)); %?
subplot(3,1,3);
plot(f, abs(X)); %Only want the positive values
title('Spectral Frequency');
xlabel('Frequency (Hz)'); ylabel('Power');
end
At this stage, I'm assuming this is correct for:
"Generate a signal with frequencies 85,150,330Hz using a sampling frequency of 1000Hz - plot 1seconds worth of the signal and its Discrete Fourier Transform."
The next step is to "Find the frequency response of an LTI system that filters out the higher and lower frequencies using the Fourier Transform". I'm stuck trying to create an LTI system that does that! I have to be left with the 150Hz signal, and I'm guessing I perform the filtering on the FFT, perhaps using conv.
My course is not a programming course - we are not assessed on our programming skills and I have minimal Matlab experience - basically we have been left to our own devices to struggle through, so any help would be greatly appreciated! I am sifting through tonnes of different examples and searching Matlab functions using 'help' etc, but since each one is different and does not have a break down of the variables used, explaining why certain parameters/values are chosen etc. it is just adding to the confusion.
Among many (many) others I have looked at:
http://www.ee.columbia.edu/~ronw/adst-spring2010/lectures/matlab/lecture1.html
http://gribblelab.org/scicomp/09_Signals_and_sampling.html section 10.4 especially.
As well as Matlab Geeks examples and Mathworks Matlab function explanations.
I guess the worst that can happen is that nobody answers and I continue burning my eyeballs out until I manage to come up with something :) Thanks in advance.
I found this bandpass filter code as a Mathworks example, which is exactly what needs to be applied to my fft signal, but I don't understand the attenuation values Ast or the amount of ripple Ap.
n = 0:159;
x = cos(pi/8*n)+cos(pi/2*n)+sin(3*pi/4*n);
d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',1/4,3/8,5/8,6/8,60,1,60);
Hd = design(d,'equiripple');
y = filter(Hd,x);
freq = 0:(2*pi)/length(x):pi;
xdft = fft(x);
ydft = fft(y);
plot(freq,abs(xdft(1:length(x)/2+1)));
hold on;
plot(freq,abs(ydft(1:length(x)/2+1)),'r','linewidth',2);
legend('Original Signal','Bandpass Signal');
Here is something you can use as a reference. I think I got the gist of what you were trying to do. Let me know if you have any questions.
clear all
close all
Fs = 1000;
t = 0:(1/Fs):1;
N = length(t);
% 85, 150, and 330 Hz converted to radian frequency
w1 = 2*pi*85;
w2 = 2*pi*150;
w3 = 2*pi*330;
% amplitudes
a1 = 1;
a2 = 1.5;
a3 = .75;
% construct time-domain signals
x1 = a1*cos(w1*t);
x2 = a2*cos(w2*t);
x3 = a3*cos(w3*t);
% superposition of 85, 150, and 330 Hz component signals
x = x1 + x2 + x3;
figure
plot(t(1:100), x(1:100));
title('unfiltered time-domain signal, amplitude vs. time');
ylabel('amplitude');
xlabel('time (seconds)');
% compute discrete Fourier transform of time-domain signal
X = fft(x);
Xmag = 20*log10(abs(X)); % magnitude spectrum
Xphase = 180*unwrap(angle(X))./pi; % phase spectrum (degrees)
w = 2*pi*(0:N-1)./N; % normalized radian frequency
f = w./(2*pi)*Fs; % radian frequency to Hz
k = 1:N; % bin indices
% plot magnitude spectrum
figure
plot(f, Xmag)
title('frequency-domain signal, magnitude vs. frequency');
xlabel('frequency (Hz)');
ylabel('magnitude (dB)');
% frequency vector of the filter. attenuates undesired frequency components
% and keeps desired components.
H = 1e-3*ones(1, length(k));
H(97:223) = 1;
H((end-223):(end-97)) = 1;
% plot magnitude spectrum of signal and filter
figure
plot(k, Xmag)
hold on
plot(k, 20*log10(H), 'r')
title('frequency-domain signal (blue) and filter (red), magnitude vs. bin index');
xlabel('bin index');
ylabel('magnitude (dB)');
% filtering in frequency domain is just multiplication
Y = X.*H;
% plot magnitude spectrum of filtered signal
figure
plot(f, 20*log10(abs(Y)))
title('filtered frequency-domain signal, magnitude vs. frequency');
xlabel('frequency (Hz)');
ylabel('magnitude (dB)');
% use inverse discrete Fourier transform to obtain the filtered time-domain
% signal. This signal is complex due to imperfect symmetry in the
% frequency-domain, however the imaginary components are nearly zero.
y = ifft(Y);
% plot overlay of filtered signal and desired signal
figure
plot(t(1:100), x(1:100), 'r')
hold on
plot(t(1:100), x2(1:100), 'linewidth', 2)
plot(t(1:100), real(y(1:100)), 'g')
title('input signal (red), desired signal (blue), signal extracted via filtering (green)');
ylabel('amplitude');
xlabel('time (seconds)');
Here is the end result...