Plot peaks for each column in a csv then fit a curve.

3 views (last 30 days)
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!

Accepted Answer

Star Strider
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')
data = 2500×7 table
Mineral Oil Glycerol CuSO4 0.625% CuSO4 1.25% CuSO4 2.5% CuSO4 5% CuSO4 10% ___________ ________ ____________ ___________ __________ ________ _________ -0.002 -0.089 0.0032 -0.0136 -0.0036 -0.0036 -0.0118 -0.00196 -0.0889 0.00324 -0.01356 -0.00358 -0.00358 -0.01178 -0.00192 -0.0888 0.00328 -0.01352 -0.00356 -0.00356 -0.01176 -0.00188 -0.0887 0.00332 -0.01348 -0.00354 -0.00354 -0.01174 -0.00184 -0.0886 0.00336 -0.01344 -0.00352 -0.00352 -0.01172 -0.0018 -0.0885 0.0034 -0.0134 -0.0035 -0.0035 -0.0117 -0.00176 -0.0884 0.00344 -0.01336 -0.00348 -0.00348 -0.01168 -0.00172 -0.0883 0.00348 -0.01332 -0.00346 -0.00346 -0.01166 -0.00168 -0.0882 0.00352 -0.01328 -0.00344 -0.00344 -0.01164 -0.00164 -0.0881 0.00356 -0.01324 -0.00342 -0.00342 -0.01162 -0.0016 -0.088 0.0036 -0.0132 -0.0034 -0.0034 -0.0116 -0.00156 -0.0879 0.00364 -0.01316 -0.00338 -0.00338 -0.01158 -0.00152 -0.0878 0.00368 -0.01312 -0.00336 -0.00336 -0.01156 -0.00148 -0.0877 0.00372 -0.01308 -0.00334 -0.00334 -0.01154 -0.00144 -0.0876 0.00376 -0.01304 -0.00332 -0.00332 -0.01152 -0.0014 -0.0875 0.0038 -0.013 -0.0033 -0.0033 -0.0115
VN = data.Properties.VariableNames;
Fs = 2e-10;
T = 1/Fs;
L = 2500;
t = (0:L-1)*T;
Fn = Fs/2;
y = table2array(data)
y = 2500×7
-0.0020 -0.0890 0.0032 -0.0136 -0.0036 -0.0036 -0.0118 -0.0020 -0.0889 0.0032 -0.0136 -0.0036 -0.0036 -0.0118 -0.0019 -0.0888 0.0033 -0.0135 -0.0036 -0.0036 -0.0118 -0.0019 -0.0887 0.0033 -0.0135 -0.0035 -0.0035 -0.0117 -0.0018 -0.0886 0.0034 -0.0134 -0.0035 -0.0035 -0.0117 -0.0018 -0.0885 0.0034 -0.0134 -0.0035 -0.0035 -0.0117 -0.0018 -0.0884 0.0034 -0.0134 -0.0035 -0.0035 -0.0117 -0.0017 -0.0883 0.0035 -0.0133 -0.0035 -0.0035 -0.0117 -0.0017 -0.0882 0.0035 -0.0133 -0.0034 -0.0034 -0.0116 -0.0016 -0.0881 0.0036 -0.0132 -0.0034 -0.0034 -0.0116
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
Alex
Alex on 4 Nov 2022
Thank you, I will continue to play around with it some more. This should work fine though for my data, i think it will really work well when I look at T2 and the free induction decay signals. I appreciate all the help truly!
Star Strider
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.

Sign in to comment.

More Answers (2)

Image Analyst
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
Alex
Alex on 4 Nov 2022
I am running into an error when I'm adding in my data, it plots it but there is so much noise I can't read the chart. Attached is the correct signal as well (I attached the wrong spreadsheet initially!) Also something to note, the time step is tau, and tau is sampled at 0.1 ms per peak after the first peak. So peak 1 is 0 peak 2 is 0.1, peak 3 0.2, and so on...
Thank you for your help!
Image Analyst
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?
You've accepted @Star Strider's answer already so are you still having trouble doing the fit?

Sign in to comment.


Alex
Alex on 4 Nov 2022
Sorry for the confusion (if it's any consolation by going through this I am understanding the experiment more.) The signal in the 'attached figure' (not the picture) that is a signal from an oscilloscope. I drew arrows indicating 'peak 1', 'peak 2' and 'peak 3' (the first couple peaks before the labeled peak 1 are noise and can be deleted.) What is done we subject a magnetic field by emitting a pulse (peak 1) then we flip that field with a second pulse at a time (tau = 0.2ms) (peak 2) then there is an echo (peak 3) at 2 tau = 0.4 (the next echo would be at 0.8ms and so on.) I am after the decaying echo peaks that I drew arrows to the majority of them (they follow a decreasing trend. This trend will allow us to find T1 our relaxation time of when the field reverts back to its normal axis.
the equations have been different in each different paper i have read, however, y = A-B *exp(-tau/T1) is used the most. in the indicates the decaying of the echo as tau increases. The field should level out so I only need to really draw it out to tau 100 ms (or shorter if it levels off before then.)
I hope this helps clarify things a bit!
  3 Comments
Alex
Alex on 6 Nov 2022
Thank you so much, I played around with the time step and reviewed my lab notes as well. I needed to account for different time steps for each sample, but they can still be plotted on the same access as the signal decays after a time!
I appreciate all the help, I am relearning Matlab and this has helped me out quite a bit!

Sign in to comment.

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!