how to draw peaks in specific locations?
Show older comments
i need to draw peaks in specific coordinations how can i do it ?
Answers (1)
Star Strider
on 2 Aug 2014
Here is one way:
P = @(x,c,w) exp(-w*(x-c).^2); % Function to calculate peaks
W = 2; % Width (constant here, can vary as a vector if you like)
C = 5:5:15; % Peak centers
x = linspace(0,20,250); % Define ‘x’ vector
pks = zeros(size(x)); % Preallocate ‘pks’
for k1 = 1:length(C) % Loop through ‘C’ (centers) vector
pks = pks + P(x,C(k1),W);
end
figure(1) % Plot
plot(x,pks)
grid
produces:

13 Comments
marc kahwaji
on 2 Aug 2014
Star Strider
on 2 Aug 2014
This does what you want:
P = @(x,y,c,w) exp(-((x-repmat(c(1),size(x,1),size(x,2))).^2 + (y-repmat(c(2),size(y,1),size(y,2))).^2)./w); % Function to calculate peaks
W = 2; % Width (constant here, can vary as a vector if you like)
C = [[5 10 15 2 7 10 12 17]; [3 6 9 12 17 7 15 13]]; % Peak centers
x = linspace(0,20,150); % Define ‘x’ vector
[X,Y] = meshgrid(x);
pks = zeros(size(X)); % Preallocate ‘pks’
for k1 = 1:size(C,2) % Loop through ‘C’ (centers) vector
pks = pks + P(X,Y,C(:,k1),W) * (-1)^k1;
end
figure(1) % Plot
meshc(X,Y,pks)
grid on
I alternated them to be positive or negative by multiplying them by (-1)^k1 simply to demonstrate that effect. Experiment with the centres and width to get the effect you want. If you want them different widths, you have to provide a vector of one width for each centre. The call to ‘P’ then becomes:
P(X,Y,C(:,k1),W(k1))
with everything else staying the same.
The code here produces:

marc kahwaji
on 4 Aug 2014
Star Strider
on 4 Aug 2014
Edited: Star Strider
on 4 Aug 2014
My pleasure!
marc kahwaji
on 4 Aug 2014
Star Strider
on 4 Aug 2014
You would need a separate vector for the amplitudes, just as for the widths.
The function changes to:
P = @(x,y,c,w,a) a .* exp(-((x-repmat(c(1),size(x,1),size(x,2))).^2 + (y-repmat(c(2),size(y,1),size(y,2))).^2)./w); % Function to calculate peaks
with the additional ‘a’ argument being the amplitude, and the call to it changes to:
P(X,Y,C(:,k1),W(k1),A(k1))
There have to be as many elements in the W and A vectors as there are columns in the C matrix.
marc kahwaji
on 4 Aug 2014
Star Strider
on 4 Aug 2014
My pleasure!
The most sincere expression of gratitude here on MATLAB Answers is to Accept the Answer that most closely solves your problem.
marc kahwaji
on 4 Aug 2014
Edited: Star Strider
on 4 Aug 2014
Star Strider
on 5 Aug 2014
Edited: Star Strider
on 5 Aug 2014
My pleasure!
I don’t believe it’s possible to overplot the contour plot you’ve created and an image. At least I can’t find it anywhere in the documentation for core MATLAB or the Image Processing Toolbox.
You might want to post the image part of your Question as a separate Question. Image Analyst will likely see it there. If he can’t do it, it can’t be done. That’s out of my area of expertise.
marc kahwaji
on 5 Aug 2014
Star Strider
on 5 Aug 2014
I see you posted that Question and Image Analyst provided an appropriate Answer. Accepting Image Analyst’s Answer and mine here would be an appropriate response from you.
marc kahwaji
on 5 Aug 2014
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!