Page 1 of 1

multifrequency esfit?

Posted: Thu Oct 15, 2015 8:02 am
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...

Re: multifrequency esfit?

Posted: Thu Oct 15, 2015 1:21 pm
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.

Re: multifrequency esfit?

Posted: Fri Oct 16, 2015 3:06 am
by Emilien
Thanks a lot!
I'll try!

Re: multifrequency esfit?

Posted: Mon Oct 19, 2015 8:33 am
by Stefan Stoll
There is a little bit about custom fitting functions in the documentation - check here.

Re: multifrequency esfit?

Posted: Tue Nov 17, 2015 2:34 am
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

Re: multifrequency esfit?

Posted: Tue Nov 17, 2015 7:51 am
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.

Re: multifrequency esfit?

Posted: Tue Nov 17, 2015 9:14 am
by AMullen
It seems that adding in a third input for the function has fixed it. Thanks for your help!

Re: multifrequency esfit?

Posted: Mon Oct 31, 2016 10:50 am
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

Re: multifrequency esfit?

Posted: Tue Apr 10, 2018 12:38 pm
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.