MATLAB: Peak addition/ arrary addition

array additionMATLAB

I have a question about the addition of peaks in the form of 2D arrays, say, each peak is a 400X2 array.
Peak 1:
P1x = [1500:0.01:1504]' % 400 points centered at 1502
P1Y = 5.*exp(-(1-x./1502).^2/0.3) % some function of x
similarly, peak2:
P2x = [1501.8:0.01:1505.8]' % 400 points centered at 1503.8
P2Y = 12.*exp(-(1-x./1503.8).^2/0.5) % some function of x
Peak 3:
P3x = [1505:0.01:1509]' % 400 points centered at 1507
P3Y = 17.*exp(-(1-x./1507).^2/0.8)
…..etc. How to do the peak addition if there is any overlap and store them in a 2D array, so X of the peak sum will be [1501:0.01:1508] ( not from 1500 to 1509); Y of the sum is just the addition of the peaks.
what if I have hundreds of peaks? Thanks

Best Answer

If all the peaks have the same parameterised formula
f = @(A,B,C,x)(A.*exp(-(1-x./B).^2./C);
for i = 1:numel(x)
y(i) = sum(f(A,B,C,x(i)));
end
Its not quick, but it will do the job for 100s of thousands of peaks. - You'll need to supply "x".
Alternatively, a lot quicker, if you use "bsxfun", and a bit of intelligence, you'll be able to get rid of the loop. - But you won't be able to handle quite so many peaks.