	function bend, x, width, derivative=derivative

;	Returns bending curve for a given (modulated) curve.
;	The result is likely to the detection process on radio signals.

N=n_elements(x)
if n_elements(width) le 0 then width=N/10

k=1-1./width


	if keyword_set(derivative) then y=smooth(abs(deriv(x)), width) else begin
y=fltarr(N)
for j=1L,N-1 do y(j)=y(j-1)*k*(x(j) lt y(j-1))+x(j)*(x(j) ge y(j-1))
	endelse

y_mean=mean(y)

FFT_y=fft(temporary(y-y_mean),-1)

FFT_y(float(N)/width:N-float(N)/width-1)=0

y=float(fft(temporary(FFT_y),1))+y_mean > 0

y=total(abs(x))/total(y)*y*sqrt(2.)

	return,y

	end