how to draw peaks in specific locations?

Answers (1)

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

no not like that i need to create peaks in specific coordinations similar to the function surf find attach the image to see the examples of peaks that i need to draw i need to draw them in specific coordinations
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:
thanks for your help :D
My pleasure!
i have one last question if i need to change the amplitude of each peak how can i do it?
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.
ok thank you very much
My pleasure!
The most sincere expression of gratitude here on MATLAB Answers is to Accept the Answer that most closely solves your problem.
i have modified a little bit the code to obtain colored circles
close all;
clear all;
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);
A=[6 8 10];
W = 1; % Width (constant here, can vary as a vector if you like)
C = [[18 15 5];[10 10 10]]; % 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,A(k1)) ;
end
A=meshz(X,Y,pks);
view(2);
grid on;
colorbar
i obtained colored circles if i need to put an image as a background for those circles how can i do it ?
Thanks for your help !!
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.
okey thanks for your help !!
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.
this is my serial data code
clear all; delete(instrfind); s = serial('COM6'); instrfind set(s, 'InputBufferSize', 256); set(s, 'FlowControl', 'none'); set(s, 'BaudRate', 9600); set(s, 'Parity', 'none'); set(s, 'Timeout',1);
disp(get(s,'Name')); prop(1)=(get(s,'BaudRate')); prop(2)=(get(s,'DataBits')); prop(3)=(get(s, 'StopBit')); prop(4)=(get(s, 'InputBufferSize'));
disp(['Port Setup Done!!',num2str(prop)]); fopen(s); instrfind
stopTime = '9/24 21:53'; time = now;
i=1; j=1;
while ~isequal(datestr(now,'mm/DD HH:MM'),stopTime)
d(i,j)=fscanf(s,'%f') %reading serial data and putting them in a matrix
e=d(:,1)' %values of the first sensor
f=d(:,end)' %values of the second sensor
subplot(1,2,1);
plot(e) %plotting the values of the first sensor
grid
subplot(1,2,2);
plot(f) %plotting the values of the second sensor
grid
j=j+1; if j==3 % if the index of columns of the matrix is 3
i=i+1; %we go to a new line of the matrix we increment the index of lines
j=1; %and we put the index of columns to 1
end ;
drawnow end; fclose(s); delete(s);
clear all;
i have serial data coming from two sensors.I'm putting them in a matrix then splitting them in two matrix: e and f. e contains the values of the first sensor and f contains the values of the second sensor. i need to draw two circles, one for each sensor, and i need to color them according to the values in matrix e and f. how can i do it?

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

on 2 Aug 2014

Commented:

on 5 Aug 2014

Community Treasure Hunt

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

Start Hunting!