hi...i have some scatter points in x-y coordinate.how can curve fitting without using of cftool?i want do it by gussian fitt but in gussian code there are x and y.i domt know how obtain them!
% f = fit(x,y,'gauss8'); % plot(f,x,y); % coeff=fit(x,y,ft);
G is sample matrix with 106*47 in size and meanValue is mean of columns in matrix.result matrix is 1*47 that it is scatter points.
meanValue = mean(G)
thank you

댓글 수: 1

Image Analyst
Image Analyst 2014년 5월 17일
A screenshot of your data, or attaching your data file, would help us envision what you're talking about.

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2014년 5월 17일

2 개 추천

Here’s my version of Gauss1 using fminsearch:
% % Single Gaussian Regression
% % Generate x- and y- data:
% x = linspace(0,10); % Generate x-data
% Normal distribution — b(1) = mean, b(2) = std
N = @(b,x) exp(-((x-b(1)).^2)./(2*b(2)^2)) ./ (b(2)*sqrt(2*pi));
% b = [5; 1];
% d1 = N(b,x); % Generate smooth y-data
% d2 = d1 + 0.05*(rand(size(d1))-.5); % Add noise
% % ————— Assign your variables to x and y here —————
x = your independent variable
y = your dependent variable
% % Use ‘fminsearch’ to do regression:
y = d2;
OLS = @(b) sum((N(b,x) - y).^2); % Ordinary Least Squares cost function
opts4 = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, rand(2,1))
Nfit = N(B,x); % Calculate estimated fit
The variables you need to have in your workspace are x and y (or assign them to x and y) to use this without modifying the code. The single ‘%’ commented lines are there for illustration. (Uncomment them if you want to see how it works. Remove them if you don’t need them.)

댓글 수: 27

I downloaded and fit the data in ‘meanValue.fig’. I changed my code to a 2-Gaussian model, and got what I consider a decent fit.
You will have to assign your x and y variables to the ones I call x and y in my code, just after my comment:
% % ————— Assign your variables to x and y here —————
after that, it should run without problems.
Initial choice of parameters, especially b(2) and b(5) in N2, is important in situations like these, since it’s possible for both Gaussian functions to fit to same peak, ignoring the other one.
I got for the fitted parameter set:
B =
584.9265
28.8754
4.3951
312.9486
38.4016
2.6977
fitting to your data with this code:
% % Double Gaussian Regression
N = @(b,x) b(1).*exp(-((x-b(2)).^2)./(2*b(3)^2)) ./ (b(3)*sqrt(2*pi));
N2 = @(b,x) b(1).*exp(-((x-b(2)).^2)./(2*b(3)^2)) ./ (b(3)*sqrt(2*pi)) + b(4).*exp(-((x-b(5)).^2)./(2*b(6)^2)) ./ (b(6)*sqrt(2*pi));
% % ————— Assign your variables to x and y here —————
x = ...;
y = ...;
% % Use ‘fminsearch’ to do regression:
OLS = @(b) sum((N2(b,x) - y).^2); % Ordinary Least Squares cost function
opts4 = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, [50 25 5 50 40 5]')
N2fit = N2(B,x); % Calculate estimated fit
Nfit(1,:) = N(B(1:3),x);
Nfit(2,:) = N(B(4:6),x);
figure(1)
plot(x, y, '+b')
hold on
plot(x, N2fit, '-r', 'LineWidth',1.5)
plot(x, Nfit(1,:), '-g')
plot(x, Nfit(2,:), '-g')
hold off
grid
text(5, 55, sprintf('Centre #1 = %.2f, \\sigma #1 = %.2f\nCentre #2 = %.2f, \\sigma #2 = %.2f', B(2:3), B(5:6)), 'FontName','Consolas', 'FontSize',7)
producing this plot:
fereshte
fereshte 2014년 5월 18일
편집: fereshte 2014년 5월 18일
dear Star Strider thanks a lot....great.very good.but i dont run it.error:
??? Error: File: Untitle.m Line: 6 Column: 3 The expression to the left of the equals sign is not a valid target for an assignment.
some Question?
1.Which line in this code read the my input image?
2.Is this code for any other data?
3.b Parameter must be calculated for each image?
4.how obtain coefficients by this code?they are important for my work.
I omitted the lines that read your image because I thought you had access to those data without them. If you want to use that and other images (nothing wrong with that), insert these lines before the comment line:
ssf = openfig('meanValue.fig')
hf2 = get(gca, 'Children');
hf3xc = get(hf2, 'XData');
hf3yc = get(hf2, 'YData');
% % Double Gaussian Regression
then:
% % ————— Assign your variables to x and y here —————
x = hf3xc;
y = hf3yc;
If you want to use other figures as sources of data, simply insert their names in the openfig line. It should run without problems, although you might need to change the values for b(2) and b(5) as I mentioned previously, to initially approximate the centres of the peaks of your data.
I’ll re-post the entirety of my code if necessary, but these are the only changes you need to make it work. The code should work for all sets of x and y data that you want to give it. Unless you use your ‘.fig’ files as input (in which situation you only need to change their names in the openfig statement), you simply have to use the code in my previous comment, plug in the names of your variables in place of hf3xc and hf3yc in my code, and adjust two of the initial parameter estimates. The program should then successfully estimate all the parameters.
fereshte
fereshte 2014년 5월 18일
The program successfully run. dear friend, thank you so much for your help.youre very clever.Good luck in your life. only if it is possible help me to get code that calculate b(2) and b(5) for other images.
Star Strider
Star Strider 2014년 5월 18일
My pleasure!
The sincerest and most valuable expression of thanks in MATLAB Answers is to Accept the Answer that is closest to answering your Question and solving your problem.
This code will calculate b(2), b(5) for all images of this type. You will have to give initial estimates for b(2), b(5) unless they are in approximately the same locations as they were in the figure you posted. In this situation, it is possible to create a function file out of my code, but I need to know more about the other images you want to analyse with it, and if you only want to analyse your images with it.
fereshte
fereshte 2014년 5월 18일
my database are footprint images of different people.I will run examples of them and compare with meanValue curve if they arent in the same locations,will post here.I've no word to express my seep gratitude.thank you for your time
Star Strider
Star Strider 2014년 5월 18일
My pleasure!
Do you have the Signal Processing Toolbox?
fereshte
fereshte 2014년 5월 18일
no
Star Strider
Star Strider 2014년 5월 18일
I asked because it has a findpeaks function that would help locate the peaks for you to estimate subsequently. You can use the max function to find the highest peak and its index (to define the x-coordinate of the peak).
If the first peak is always the highest, set that initial estimate as b(2), and define b(5)=b(2)+10 for its initial estimate.
Simply an idea to make your analysis easier!
fereshte
fereshte 2014년 5월 19일
thanks a lot
Star Strider
Star Strider 2014년 5월 19일
My pleasure!
fereshte
fereshte 2014년 5월 19일
dear Star Strider i attached other curve in new Comment.please see it.
fereshte
fereshte 2014년 5월 19일
in cftool, gauss 8 give me best fit of curve. this program generate a code for gauss 8 but i dont run it.i need to code to run for every image of right and left footprint.i dont write in command window cftool for every image and run it.its Boring.
Star Strider
Star Strider 2014년 5월 19일
I don’t have the curve fitting toolbox (I don’t need it with the Statistics and Optimization Toolboxes) so I can’t help you with it.
Did setting the initial values to b(2)=20, b(5)=40 work?
fereshte
fereshte 2014년 5월 19일
no
Star Strider
Star Strider 2014년 5월 19일
If I have time today, I’ll expand the two-peak plot to four and let you experiment with it.
fereshte
fereshte 2014년 5월 19일
can i find x-coordinate of peaks by this code?
[~,locs_Rwave] = findpeaks(meanValue);
fereshte
fereshte 2014년 5월 19일
ok.thank you.
Star Strider
Star Strider 2014년 5월 21일
Yes. The findpeaks function would definitely work.
fereshte
fereshte 2014년 5월 21일
thanks
Star Strider
Star Strider 2014년 5월 21일
My pleasure!
fereshte
fereshte 2014년 5월 22일
편집: fereshte 2014년 5월 22일
Sorry. by this code,i get the peak values, but sometimes I obtain 3 or 4 values for peak   1- Which values of these important(max of them)?
2-Is there the code to replace these values in the formula and I do not need to enter them manually for each of the image?
fereshte
fereshte 2014년 6월 4일
Star Strider
how obtain b(1),...b(5) for any image? i only get b(2) and b(5) by [~,locs_Rwave] = findpeaks(meanValue);
Star Strider
Star Strider 2014년 6월 4일
I suggest you fit them as I did in my code. Use estimates for b(2) and b(5) from findpeaks as your initial parameter estimates for those elements in the call to fminsearch.
fereshte
fereshte 2014년 6월 4일
편집: fereshte 2014년 6월 4일
thanks.this way is correct for soe images.but now i have a problem for one of them.i attached it in new post.please help me for values of b(1) and b(5) in this image.
Image Analyst
Image Analyst 2014년 6월 29일
Please put that "Answer" right here. No need to put a comment here and then say that Star has to look somewhere else down the page for it, especially since it's not really an "Answer" to your original post but a comment to Star.
fereshte
fereshte 2014년 6월 29일
편집: fereshte 2014년 6월 29일
dear Star Strider
i use youre code for my dataset. but it dont work for all images. i change B line in this code respect to locs_Rwave for some images that dont get me true response.but its very difficult.can you help me to get the code that run perfectlly for all images with out any change in B line?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 5월 17일

0 개 추천

How about polyfit. I've attached a demo.

질문:

2014년 5월 17일

편집:

2014년 6월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by