How can I align multiple curves on one plot to a central point?

45 Ansichten (letzte 30 Tage)
I am plotting several curves on a single plot as part of several test points for a tool I am developing. In the final step, I overlay all the curves on top of one another for a visual display. As you can see all the plots are shifted relative to one another. How can i go about aligning these cruves so that similar features align? Keep in mind they will not perfectly align. What I am looking at was something like the pdf file (done in excel). I thought I could find the center point for the flat portion of the curve (already have those points defined as part of the test) and somehow make all 15 curves center point align on the curve. Any ideas are appreciated.
Edit:Updated question and attachments based on feedback.
Thanks
Sample Code:
for ii =1:length(SNSource)
%import power data for each product
ScatterData=importdata(cell2mat(SNSource(ii)))
plot(ScatterData(:,1),ScatterData(:,2))
title('ScatterData')
hold on
xlabel('Distance mm')
ylabel('Power')
end

Akzeptierte Antwort

Image Analyst
Image Analyst am 3 Jan. 2023
How about if you found where it crossed 0.001 on both sides, compute the middle from that, and then compute the delta x from there to the desired, common x center point and add it to the x vector for each curve?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
  5 Kommentare
Image Analyst
Image Analyst am 6 Jan. 2023
It was not a typo. Your original workbook didn't have x values in column 1 go all the way down to as far as the lowest row for any of the y values, so I had to see the pattern and create the x values you forgot to include.
I want to find the index, and then the x values at that index, where the pulse if above the middle y point to get the full width half max. FWHM is a common metric for describing where a pulse starts and stops. So I wanted to find those points to find the center x point of the pulse. This way, by looking only at y values above the noise, my pulse width won't be affected by any noise because the noise will be below the half way up value.
To shift things to a new center you need to subtract the existing center, then scale it and then add in the center that you want. For example let's say that x went from 100 to 106 pixels, so it has a center of 103 and a width of 7. So now you subtract 103 and you get x going from -3 to +3 but still with a width of 7 pixels. Now you can scale. Let's say you want to double the width so multiply x by 2 so now x goes from -6 to +6 with a width of 13 pixels (if you cound pixels rather than going from center to center). So now if you want the new center at 50, you add 50 so now x goes from 44 to 56. It now has the new, scaled width, and the center is at the desired location.
SnooptheEngineer
SnooptheEngineer am 10 Jan. 2023
Thank you very much for explaing this in great detail. Makes much more sense now :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

William Rose
William Rose am 3 Jan. 2023
It would help if you would attach the actual data as a .mat file or a text file for easy import.
Your choce of a linear y-axis for one plot, and a logarithmic y-axis for the other, makes it more difficult to compare the images.
The first image file attached appears to be the one from Excel, but I am not positive, and file names and plot titles do not make it obvious. I recommend that in the future you not use terms like "the second picture", since the order of file attachment may not be what you expect.
It looks to me like you have already aligned the plots pretty well in the Excel plot. When you say you want to align the plots better, are you only doing a horizontal shift? Or do you also want to do a vertical shift? Would you allow shrinking or stretching of the horizontal or vertical scale to improve the alignment?
I will assume you just want to slide the curves horizontally, without steching or shrinkking. Here is an example of how you can use the cross correlation to adjust the horizontal position, using simulated data:
%create simulated data
N=100;
t1=1:N; t2=t1;
x1=10*[zeros(1,35),ones(1,20),zeros(1,45)]+rand(1,N);
x2=10*[zeros(1,45),ones(1,20),zeros(1,35)]+rand(1,N);
%find the optimal alignment for the simulated data
[~,idx]=max(xcorr(x1,x2)); %idx=index where cross correlation is maximum
t2adj=t2+idx-N; %shift the time base for x2
%plot the results
subplot(211); plot(t1,x1,'-r.',t2,x2,'-b.');
legend('x1','x2'); title('Raw Data');
subplot(212); plot(t1,x1,'-r.',t2adj,x2,'-b.');
legend('x1','x2'); title('Adjusted Data');
This works nicely. You can do it again for x1 and x3, x1 and x4, etc. Good luck!
  1 Kommentar
SnooptheEngineer
SnooptheEngineer am 4 Jan. 2023
Thank you very much for your response. I have updated my question above. I am sorry for the lack of clarity. The issue is that these products are not all aligned the same on the system. They are all scanned over a the same distance but the saturated peak( horizontal segement) occurs at different segements of the scan range. You are correct as I do want to shift the curves left-right so that they align on top of each other. I realize they wont be perfect but more presentable and easier to view.
My thought process was computing the mid point for the horizontal segement. Choosing a point on the x axis then having align midpoints for each curve align on that point. I am not sure if that can be even done in matlab plot.
Now to your proposed solution.I got a little confused by the code. First, you find a correlation between te two curves. Would that be the maximum value for both? Then you shift the time vector ( x axis ) by that value so they align? How would that be done for 10, 20 ,30 curves. Would there be one value or several to shift by?
Thanks again for your help and response

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by