multifrequency esfit?

General forum for EasySpin: questions, how to's, etc.
Post Reply
Emilien
Resident User
Posts: 62
Joined: Mon Apr 20, 2015 12:13 am

multifrequency esfit?

Post by Emilien »

Hi,

I know that some fitting programs can perform several correlated simulations at the same time.
For example, it could be of interest to simulate spectra of a given sample recorded at multiple frequencies with the same parameters.
I don't know how difficult it is, but it could be great to add this possibility to esfit...
One can dream...
Matt Krzyaniak
EasySpin Guru
Posts: 153
Joined: Tue Jul 22, 2014 11:01 am
Location: Northwestern University

Re: multifrequency esfit?

Post by Matt Krzyaniak »

You can do that, without too much difficulty.
First you create a new function such as

Code: Select all

function y = multifreq(Sys,Exp)

% you could also hardcode some of this
Exp.Range = [Exp.f(1) Exp.f(2)];
Exp.nPoints = Exp.n(1); 
Exp.mwFreq = Exp.mw(1);
freq1 = pepper(Sys,Exp);

Exp.Range = [Exp.f(3) Exp.f(4)];
Exp.nPoints = Exp.n(2);
Exp.mwFreq = Exp.mw(2);
freq2 = pepper(Sys,Exp);

y = [freq1(:); freq2(:)];

return
Then you setup the problem with:

Code: Select all

clear
[b1,y1,p1] = eprload(somefilelocationbrukerformat);
[b2,y2,p2] = eprload(somefilelocationbrukerformat);

y = [y1(:); y2(:)];

% setup your spin system
Sys.S = 1;
Sys.g = 2.004;
D = 230;
E = 0;
Sys.D = [D E];
Sys.lwpp = 2;
% setup the Experimental portion, but we need to tweak a bit here

% we need to provide these even though we overwrite them later(I think)
Exp.mwFreq = Pars.MWFQ/10^9;
Exp.Range = [min(y1) max(y2)];
Exp.nPoints = length(y);

% then we setup the actual experimental bits we'll use
Exp.f = [min(b1) max(b1) min(b1) max(b1)];
Exp.n = [length(b1) length(b2)];
Exp.mw = [p1.MWFQ/10^9 p2.MWFQ/10^9];

% and toss it all into esfit
Vary.g = 0.01;
Vary.D = [100 70];
Vary.lwpp = 1;

esfit('multifreq',y,Sys,Vary,Exp)

You can use similar tricks with phony variables in Sys, assign them correctly in your custom function and vary them within esfit.
Emilien
Resident User
Posts: 62
Joined: Mon Apr 20, 2015 12:13 am

Re: multifrequency esfit?

Post by Emilien »

Thanks a lot!
I'll try!
Stefan Stoll
EasySpin Creator
Posts: 1059
Joined: Mon Jul 21, 2014 10:11 pm
Location: University of Washington

Re: multifrequency esfit?

Post by Stefan Stoll »

There is a little bit about custom fitting functions in the documentation - check here.
AMullen
User
Posts: 14
Joined: Mon Nov 16, 2015 8:58 am

Re: multifrequency esfit?

Post by AMullen »

Hi,

I've been trying to do something similar and the examples above have helped a great deal. However, I've run into an issue when it gets to actually running a fitting function. As soon as I hit 'run' I receive an error telling me the 'multifreq' function has too many input arguments. The scripts I have been running are modified versions of the ones above, just using appropriate values for my system and the 'cwzero' function from EPRToolbox; if you want me to post these let me know. Any help or insight would be greatly appreciated.

Thanks,
Anna
Matt Krzyaniak
EasySpin Guru
Posts: 153
Joined: Tue Jul 22, 2014 11:01 am
Location: Northwestern University

Re: multifrequency esfit?

Post by Matt Krzyaniak »

That sounds like you might be trying to pass an options structure or you provided esfit with an empty structure for options, you can fix that by giving the multifreq function a third input, even if you don't actually use it.
AMullen
User
Posts: 14
Joined: Mon Nov 16, 2015 8:58 am

Re: multifrequency esfit?

Post by AMullen »

It seems that adding in a third input for the function has fixed it. Thanks for your help!
hmalissa
Newbie
Posts: 3
Joined: Wed Feb 17, 2016 10:14 am

Re: multifrequency esfit?

Post by hmalissa »

Matt Krzyaniak wrote:You can do that, without too much difficulty.
First you create a new function such as

Code: Select all

function y = multifreq(Sys,Exp)

% you could also hardcode some of this
Exp.Range = [Exp.f(1) Exp.f(2)];
Exp.nPoints = Exp.n(1); 
Exp.mwFreq = Exp.mw(1);
freq1 = pepper(Sys,Exp);

Exp.Range = [Exp.f(3) Exp.f(4)];
Exp.nPoints = Exp.n(2);
Exp.mwFreq = Exp.mw(2);
freq2 = pepper(Sys,Exp);

y = [freq1(:); freq2(:)];

return
Then you setup the problem with:

Code: Select all

clear
[b1,y1,p1] = eprload(somefilelocationbrukerformat);
[b2,y2,p2] = eprload(somefilelocationbrukerformat);

y = [y1(:); y2(:)];

% setup your spin system
Sys.S = 1;
Sys.g = 2.004;
D = 230;
E = 0;
Sys.D = [D E];
Sys.lwpp = 2;
% setup the Experimental portion, but we need to tweak a bit here

% we need to provide these even though we overwrite them later(I think)
Exp.mwFreq = Pars.MWFQ/10^9;
Exp.Range = [min(y1) max(y2)];
Exp.nPoints = length(y);

% then we setup the actual experimental bits we'll use
Exp.f = [min(b1) max(b1) min(b1) max(b1)];
Exp.n = [length(b1) length(b2)];
Exp.mw = [p1.MWFQ/10^9 p2.MWFQ/10^9];

% and toss it all into esfit
Vary.g = 0.01;
Vary.D = [100 70];
Vary.lwpp = 1;

esfit('multifreq',y,Sys,Vary,Exp)

You can use similar tricks with phony variables in Sys, assign them correctly in your custom function and vary them within esfit.
Hi Matt,

Thanks a lot, this is very useful for me.
One question about rescaling in this context: what if the spectra at different frequencies have been measured under non-identical experimental conditions, and the amplitudes differ? In this case, the spectra would need to be rescaled individually. I assume that the 'rescale' function would do the trick, but I'm not sure how to implement this in the user-defined simulation function. Could you please comment on this?
Thanks a lot,

Hans
hmalissa
Newbie
Posts: 3
Joined: Wed Feb 17, 2016 10:14 am

Re: multifrequency esfit?

Post by hmalissa »

hmalissa wrote:
Matt Krzyaniak wrote:You can do that, without too much difficulty.
First you create a new function such as

Code: Select all

function y = multifreq(Sys,Exp)

% you could also hardcode some of this
Exp.Range = [Exp.f(1) Exp.f(2)];
Exp.nPoints = Exp.n(1); 
Exp.mwFreq = Exp.mw(1);
freq1 = pepper(Sys,Exp);

Exp.Range = [Exp.f(3) Exp.f(4)];
Exp.nPoints = Exp.n(2);
Exp.mwFreq = Exp.mw(2);
freq2 = pepper(Sys,Exp);

y = [freq1(:); freq2(:)];

return
Then you setup the problem with:

Code: Select all

clear
[b1,y1,p1] = eprload(somefilelocationbrukerformat);
[b2,y2,p2] = eprload(somefilelocationbrukerformat);

y = [y1(:); y2(:)];

% setup your spin system
Sys.S = 1;
Sys.g = 2.004;
D = 230;
E = 0;
Sys.D = [D E];
Sys.lwpp = 2;
% setup the Experimental portion, but we need to tweak a bit here

% we need to provide these even though we overwrite them later(I think)
Exp.mwFreq = Pars.MWFQ/10^9;
Exp.Range = [min(y1) max(y2)];
Exp.nPoints = length(y);

% then we setup the actual experimental bits we'll use
Exp.f = [min(b1) max(b1) min(b1) max(b1)];
Exp.n = [length(b1) length(b2)];
Exp.mw = [p1.MWFQ/10^9 p2.MWFQ/10^9];

% and toss it all into esfit
Vary.g = 0.01;
Vary.D = [100 70];
Vary.lwpp = 1;

esfit('multifreq',y,Sys,Vary,Exp)

You can use similar tricks with phony variables in Sys, assign them correctly in your custom function and vary them within esfit.
Hi Matt,

Thanks a lot, this is very useful for me.
One question about rescaling in this context: what if the spectra at different frequencies have been measured under non-identical experimental conditions, and the amplitudes differ? In this case, the spectra would need to be rescaled individually. I assume that the 'rescale' function would do the trick, but I'm not sure how to implement this in the user-defined simulation function. Could you please comment on this?
Thanks a lot,

Hans
We've recently implemented a workaround for the scaling issue. Essentially, we use 'rescale' at each frequency to rescale the model to our data. That eliminates the need for the amplitude factors - possibly a large number of additional fit parameters. We describe the EasySpin code in the Supplemental Material to https://doi.org/10.1103/PhysRevB.97.161201 (section 8.3). I hope this helps.
Post Reply