Skip to content

Instantly share code, notes, and snippets.

@MarcinKonowalczyk
Last active June 30, 2020 18:10
Show Gist options
  • Save MarcinKonowalczyk/9bedc06f314ecb9ba99b7026cfade72d to your computer and use it in GitHub Desktop.
Save MarcinKonowalczyk/9bedc06f314ecb9ba99b7026cfade72d to your computer and use it in GitHub Desktop.
[Matlab] Model functions
function y = asymmetric_gaussian_lineshape(x,center,width_left,width_right,height)
%% y = asymmetric_gaussian_lineshape(x,center,width,height)
% Piecewise asymmetric Gaussian lineshape.
% 'width_left' and 'width_right' correspond to full width
% at half maximum of the respective halves. The derivative of this
% function is discontinuous at x=center.
l42 = 2.772588722239781; % 4.*log(2)
x = x-center;
y1 = exp( -l42.*(x./width_left).^2 ) .* height;
y2 = exp( -l42.*(x./width_right).^2 ) .* height;
y = y1.*(x<0) + y2.*(x>=0);
end
function y = exponential_step(x,tau,alpha,beta,center)
%% y = exponential_step(x,tau,alpha,beta,center)
% Exponential function 'decaying' from alpha to beta
% with a time constant of tau.
y = (alpha-beta) .* exp(-(x-center)./abs(tau)) + beta;
y(x<=center) = alpha;
end
function y = exponential_step_wave(x,tau,alpha,beta,dc,period,phase)
%% exponential_step_wave(x,tau,alpha,beta,dc,period,phase)
% Periodic square-wave-like function where each step decays/grows
% from alpha to beta with a time constant of tau.
% Set some default parameters
if nargin < 7 || isempty(phase), phase = 0; end
if nargin < 6 || isempty(period), period = 1; end
if nargin < 5 || isempty(dc), dc = 0; end
% Make the function
carrier = sin(2*pi*x/period + phase) > 0; % Carrier square wave
periodic_x = mod(x+phase/pi*period/2,period/2); % Periodic x axis
y = (alpha-beta) .* exp(-periodic_x./abs(tau)) + beta;
y = (carrier.*y) + (~carrier.*(-y)) + dc;
end
function y = gaussian_lineshape(x,center,width,height)
%% y = gaussian_lineshape(x,center,width,height)
% Generalised Gaussian lineshape.
% 'width' corresponds to full width at half maximum.
l42 = 2.772588722239781; % 4.*log(2)
y = exp( -l42.*((x-center)./width).^2 ) .* height;
end
function y = havercosine_lineshape(x,center,width,height)
%% y = havercosine_lineshape(x,center,width,height)
% Generalised Havercosine lineshape.
% 'width' corresponds to full width at half maximum.
region = x>=center-width & x<=center+width;
y = zeros(size(x));
y(region) = ( 1 + cos( (x(region)-center)/width*pi ) ) / 2 * height;
end
function y = lorentzian_lineshape(x,center,width,height)
%% y = lorentzian_lineshape(x,center,width,height)
% Generalised Lorentzian lineshape.
% 'width' corresponds to full width at half maximum.
y = (1./(1+((x-center)/width*2).^2)) .* height;
end
function y = sech_lineshape(x,center,width,height)
%% y = sech_lineshape(x,center,width,height)
% Generalised Hyperbolic secant lineshape.
% 'width' corresponds to full width at half maximum.
l22s3 = 2.633915793849633; % 2*log(2+sqrt(3))
y = sech((x-center)/width * l22s3) .* (height);
end
function y = sigmoid_step(x,alpha,beta,center,slope)
%% y = sigmoid_step(x,alpha,beta,center,slope)
% Sigmoid from 'alpha' to 'beta' with halfway point
% with 'slope' at 'center'.
slope = abs(slope); % Slope determined by the 'alpha' and 'beta' parameters
if alpha == beta
y = ones(size(x)).*alpha;
return
elseif alpha < beta
y = (beta-alpha)./(1+exp(-(x-center)*slope*4)) + alpha;
else % alpha > beta
y = -(alpha-beta)./(1+exp(-(x-center)*slope*4)) + alpha;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment