MATLAB: How can i make envelope cover first half peak at origin

envelope

How can i get the envelope to cover the first maxima at x=0. Here is the code.
fluxQuantum = 2.06783383E-15;
lambda = 90E-9;
L = 500E-9; w = 500E-9; d = 10E-9; N = 500;
k = 2*pi*(2*lambda+d)/fluxQuantum;
J0 = 1E7;
theta=0
pulse = @(t,a)(heaviside(t+a) - heaviside(t-a));
J1 = @(x,y) J0*pulse(x,L/2)*pulse(y,w/2);
Bwidth=0.15;
N=500;
n=91;
B = linspace(-1,Bwidth,N);
Ic_max = zeros(size(B));
Bwidth = 0.15;
c = cosd(theta);
s = sind(theta);
B = linspace(0,Bwidth,N);
By = c*B;
Bx = s*B;
kx = k*By; ky = k*Bx;
for i=1:N
% Ix(i) = integral(@(y)(J1(xvec(i),y)*exp(-1j*ky(i)*y)),-w/2,w/2,'ArrayValued',true);
f = @(x)integral(@(y)(J1(x,y).*exp(-1j*ky(i)*y)),-w/2,w/2,'ArrayValued',true);
Ic_max(i) = abs(integral(@(x)f(x).*exp(1j*kx(i)*x),-L/2,L/2));%,'ArrayValued',true);
end
[up,lo]= envelope(Ic_max,10,'peak');
plot(up)
hold on
plot(Ic_max)

Best Answer

The envelope function apparently does not recognise the centre peak as a peak, since it is at one end of the sequence.
You can get around that limitation and calculate the envelope of the centre peak if you give envelope the complete sequence to work with, or at least a sequence that does not begin with the centre peak.
This requires slightly tweaking your code:
<... PREVIOUS CODE ...>
B = linspace(-Bwidth,Bwidth,N*2);
By = c*B;
Bx = s*B;
kx = k*By; ky = k*Bx;
for i=1:numel(B)
% Ix(i) = integral(@(y)(J1(xvec(i),y)*exp(-1j*ky(i)*y)),-w/2,w/2,'ArrayValued',true);
f = @(x)integral(@(y)(J1(x,y).*exp(-1j*ky(i)*y)),-w/2,w/2,'ArrayValued',true);
Ic_max(i) = abs(integral(@(x)f(x).*exp(1j*kx(i)*x),-L/2,L/2));%,'ArrayValued',true);
end
[up,lo]= envelope(Ic_max,10,'peak');
plot(up(B>=0))
hold on
plot(Ic_max(B>=0))
to produce:
How can i make envelope cover first half peak at origin - 2019 07 09.png
Many MATLAB functions (such as findpeaks) also do not consider the end values of a vector in their calculations, so the coding that calls them must take this into account.