Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correctphase prone to inverting signal. #114

Closed
mtessmer opened this issue Mar 24, 2021 · 5 comments · Fixed by #131
Closed

correctphase prone to inverting signal. #114

mtessmer opened this issue Mar 24, 2021 · 5 comments · Fixed by #131
Labels
enhancement New feature or request
Milestone

Comments

@mtessmer
Copy link
Collaborator

Using deerlab.correctphase inverts the attached nutation experiment.

Nutation.zip

import deerlab as dl
import matplotlib.pyplot as plt
t, V = dl.deerload('Nutation.DTA')
Vph = dl.correctphase(V)
Vtrue = -Vph

plt.plot(t, V, label='Raw Data')
plt.plot(t, Vph, label='DeerLab Phased')
plt.plot(t, Vtrue, label='Correctly Phased')
plt.legend()
plt.show()

Nutation

@stestoll
Copy link
Collaborator

In this particular example, the raw data already is inverted (blue trace), and correctphase changes it just minimally by about 7 degrees (orange trace).

The basic assumption of correctphase is that the signal is more or less phased as desired, and only small adjustments need to be done. correctphase performs a least-squares minimization of the phase, minimizing the norm of the imaginary component, and using the average phase of the raw data as a starting point.

Should we adjust this behavior?

@mtessmer
Copy link
Collaborator Author

It could be useful to change the behavior for scripted data sets that do not allow users to set the phase. This nutation experiment was run as part of a prodel script and the phase changes as the script progresses

@stestoll stestoll added this to the 0.13.0 milestone Mar 29, 2021
@stestoll
Copy link
Collaborator

Indeed, the norm of the imaginary part has two minima as a function of phase angle, so the solution provided by correctphase is not unique - if there is a minimum at ph, then there is one at ph+pi as well.

The current approach picks the minimum closest to zero phase angle. Instead, as @mtessmer suggests, correctphase could pick the minimum with the larger integral of the real part of the phased signal.

We could implement this as new default behavior, with a keyword argument to switch it off. For example: selphase=maxrealint for the new (default) behavior, and selphase=minphase for the current behavior.

@stestoll
Copy link
Collaborator

correctphase should do the following:

  1. Determine an optimal phase by minimizing the norm of the imaginary part, using the average phase as starting point.
  2. Generate the other solution by adding or subtracting pi from the minimizing phase.
  3. Look at the value of the phase keyword argument to determine which of the two phases to use.

We can extend the use of the phase keyword for this.

  • phase='posrealint' - pick phase that gives a positive integral of the real part (default)
  • phase='minrealint' - pick phase that gives a negative integral of the real part
  • phase='close' - pick phase closer to initial average phase
  • phase= some number - skip optimization and use the provided phase angle

I am not sure we need the last use case (user-provided phase).

luisfabib added a commit that referenced this issue Apr 1, 2021
…rection (#131)

* correctphase: implement optimization based on integral of real component (#114)

* update changelog

* correctphase: fix wrong keyword name in docstring

* correctphase: implement phase selection, deprecate offset correction, major code refactoring

* adjust beginner's guide to new functionality of correctphase

* noiselevel: fix call to correctphase

* update changelog

* fix typos
@stestoll
Copy link
Collaborator

stestoll commented Mar 9, 2022

This keyword argument was eliminated in 3d0919c. The function returns the minimizer phase that yields a positive average of the real part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants