# How to plot a frequency of respiration signal on y-axis while time of measurement is on x-axis?

28 views (last 30 days)

Show older comments

##### 2 Comments

### Accepted Answer

Star Strider
on 10 Feb 2018

This should get you started:

D = load('respiratory.txt');

Fs = 25; % Sampling Frequency (Hz)

Fn = Fs/2; % Nyquist Frequency

Ts = 1/Fs; % Sampling Time (sec)

L = numel(D);

t = linspace(0, L, L)*Ts; % Time Vector (sec)

figure(1)

plot(t, D)

grid

% axis([0 60 -850 -750])

axis([xlim -850 -750])

xlabel('Time')

ylabel('Amplitude')

FTD = fft(D-mean(D))/L; % Fourier Transform

Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector

Iv = 1:numel(Fv); % Index Vector

figure(2)

plot(Fv, abs(FTD(Iv))*2)

grid

axis([0 2.5 ylim])

xlabel('Frequency (Hz)')

ylabel('Amplitude')

Wp = [0.35 0.65]/Fn; % Passband Frequency (Normalised)

Ws = [0.30 0.75]/Fn; % Stopband Frequency (Normalised)

Rp = 1; % Passband Ripple (dB)

Rs = 50; % Stopband Ripple (dB)

[n,Ws] = cheb2ord(Wp,Ws,Rp,Rs); % Filter Order

[z,p,k] = cheby2(n,Rs,Ws); % Filter Design, Sepcify Bandpass

[sos,g] = zp2sos(z,p,k); % Convert To Second-Order-Section For Stability

figure(3)

freqz(sos, 2^16, Fs) % Filter Bode Plot

D_filtered = filtfilt(sos, g, D); % Filter Signal

[pks,locs] = findpeaks(D_filtered, 'MinPeakDist',40);

figure(4)

plot(t, D_filtered)

hold on

plot(t(locs), pks, '^r')

hold off

grid

% axis([0 60 ylim])

axis([0 60 -15 15])

xlabel('Time')

ylabel('Amplitude')

tdif = diff([0 t(locs)]); % Time Difference Between Peaks (sec)

Dfrq = 60./tdif; % Frequency (Respirations/Minute)

figure(5)

plot(t(locs), Dfrq)

grid

axis([xlim 10 40])

xlabel('Time (sec)')

ylabel('Frequency (minute^{-1})')

I do not intend this to be a complete solution. It does some signal processing and preliminary data analysis. Make necessary changes to get the result you want. See the documentation on the various functions to understand how the code works.

Have fun!

##### 5 Comments

Star Strider
on 31 May 2020

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!