Plot peaks for each column in a csv then fit a curve.
3 views (last 30 days)
Show older comments
I am trying to process some data obtained from an oscilloscope. The columns in the csv represent the amplitude of the signal. I expected to see what is in the picture. I want to find the peaks for each signal and then plot the peaks as a function of tau (ms) ranging from 0.1 and up. I'd also like to fit a curve to each line following the exponential function y = a*e(-x/T1) +c, where a is a constant (-2) and T1 is found from the curve. Finding T1 is the ultimate goal of plotting the signals. I have tried a few different methods: here is my most recent attempt and it did not give me anywhere near what I was hoping for:
filename = 'D:\Grad Lab\NMR\Data\T1 Data\compiledT1nolabel.csv';
data = readtable(filename);
y1= table2array(data(:,1));
y2= table2array(data(:,2));
y3= table2array(data(:,3));
y4= table2array(data(:,4));
y5= table2array(data(:,5));
y6= table2array(data(:,6));
y7= table2array(data(:,7));
Fs = 2e-10;
T = 1/Fs;
L = 2500;
t = (0:L-1)*T;
Fn = Fs/2;
Fy1 = fft(y1)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:length(Fv);
figure(1)
plot(Fv, abs(Fy1(Iv))*2)
grid
title("Fourier Transfrom of Mineral Oil Original Signal")
xlabel('Frequency (Hz)')
ylabel('Amplitude')
Fy1dcoc = fft(y1-mean(y1))/L;
figure(2)
plot(Fv, abs(Fy1dcoc(Iv))*2)
grid
title('Fourier Transform of D-C Offset Corrected Signal')
xlabel('Frequency (Hz)')
ylabel('Amplitude')
[Fy1n_max,Iv_max] = max(abs(Fy1dcoc(Iv))*2);
Wp = 2*Fv(Iv_max)/Fn;
Ws = Wp*2;
Rp = 10
Rs = 30;
[n,Wn] = buttord(Wp,Ws,Rp,Rs);
[b,a] = butter(n,Wn);
[SOS,G] = tf2sos(b,a);
S = filtfilt(SOS,G,y1);
figure(4)
plot(t, y1)
hold on
plot(t, S, '-r', 'LineWidth',1.5)
hold off
grid
legend('Mineral oil', "'S'", 'Location', 'N')
title('Original Signal of Mineral Oil and Uncorrupted Signal (S)')
xlabel('Tau (ms)')
ylabel('Amplitude')
I appreciate any help with this!

0 Comments
Accepted Answer
Star Strider
on 3 Nov 2022
There are no visible peaks in the time-domain signal.
Doing the Foureir transform and finding the peaks is easy enough, however I have no idea how to do the plot in the image because I have no idea how to calculate whatever the variables are that it depicts. The independent variable appears to be time.
If your intent is to do a time-frequency analysis, it would be best to use the pspectrum function with the 'spectrogram' option, rather than fft.
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1178628/compiledT1.csv', 'VariableNamingRule','preserve')
VN = data.Properties.VariableNames;
Fs = 2e-10;
T = 1/Fs;
L = 2500;
t = (0:L-1)*T;
Fn = Fs/2;
y = table2array(data)
figure
plot(t, y)
grid
xlabel('Time')
ylabel('Amplitude')
legend(VN, 'Location','best')
ydt1 = detrend(y(:,1),1);
figure
plot(t, ydt1)
grid
xlabel('Time')
ylabel('Amplitude')
title(VN{1})
[p,f,t] = pspectrum(ydt1,Fs,'spectrogram');
figure
waterfall(f,t,p')
xlabel('Frequency (Hz)')
ylabel('Time (seconds)')
wtf = gca;
wtf.XDir = 'reverse';
colormap(turbo)
view([30 45])
NFFT = 2^nextpow2(L);
ym = y - mean(y);
FTy = fft(ym, NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
figure
plot(Fv, abs(FTy(Iv,:))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
legend(VN, 'Location','best')
xlim([0 5E-12])
[pks1,locs1] = findpeaks(abs(FTy(Iv,1))*2);
figure
plot(Fv, abs(FTy(Iv,1))*2, 'DisplayName','Data')
hold on
plot(Fv(locs1), pks1, '^r', 'DisplayName','Peaks')
hold off
grid
xlabel('Frequency')
ylabel('Magnitude')
title(VN{1})
legend()
% legend(VN{1}, 'Location','best')
xlim([0 5E-12])
.
9 Comments
Star Strider
on 4 Nov 2022
My pleasure!
I also don’t understand the time values on the x-axis in the figure in your last Comment. In my code, I assigned each peak x-value a serial 0.1 millisecond value. That may not be correct, however that’s how I interpreted ‘So for each peak that is a one tau (0.1ms.)’ although it doesn’t make sense to me.
More Answers (2)
Image Analyst
on 3 Nov 2022
See attached demo. It will be easy for you to adapt it to your data. Just replace demo data with your actual data.
6 Comments
Image Analyst
on 6 Nov 2022
There are 7 columns in that workbook. What do the different columns represent? Which column is the x value and which are/is the y value(s)? What column are you trying to fit with an exponential?
See Also
Categories
Find more on Measurements and Feature Extraction in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


















