Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Added AnalogQuantity.square_wave(). #77

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

Merged
merged 2 commits into from
Nov 10, 2021
Merged

Conversation

zakv
Copy link
Contributor

@zakv zakv commented Feb 26, 2021

This PR adds support for a square_wave() waveform for analog outputs. It is of course already possible to achieve a square wave output using AnalogQuantity.customramp(), but getting it right takes a bit of thought, particularly accounting for rounding errors in the timings. Therefore it's useful to have a built-in method for them.

The arguments that set the output values are called level_0 and level_1 which isn't the prettiest. I went back and forth over a few different ideas on how users should set the output levels but decided that was the most clear and simple way. I think amplitude and offset was somewhat ambiguous (is offset the midpoint or low level? Is amplitude peak-peak or is it half that like a sine wave?). I also think high_level and low_level isn't ideal because then you'd probably want another option like start_high=False, which can be misleading if the initial phase isn't zero, and what should be done if the user sets high_level to something smaller than low_level? Also, initial_level and final_level aren't great since the actual final output value may be equal to initial_level if there is a non-integer number of cycles. I'm all ears though if someone has a better suggestion for those arguments.

@philipstarkey
Copy link
Member

philipstarkey commented Feb 26, 2021

Seems like a good idea!

What about parameterising it with an amplitude and offset? That might be more consistent with the other functions and solve the kwarg naming concerns?

@philipstarkey
Copy link
Member

...just realised you covered that in your message. Oops sorry. Ignore me!

@zakv
Copy link
Contributor Author

zakv commented Feb 26, 2021

No worries, amplitude and offset might still be a better way to go despite the slight ambiguity given that the docstring will explain what they mean. In that case it might make sense to have the phase go from 0 to 2 pi then for better analogy with sine.

@dihm
Copy link
Contributor

dihm commented Nov 8, 2021

So, I think this is probably pretty easy to get merged and would love to do so. @zakv, did you come down on a choice for how to paramterize?

My 2 cents (feel free to ignore), is to use amplitude and offset. That is most familiar to me as it closely approximates the standard behavior of every function generator I've ever used. As you say, a docstring should be more than enough to stamp out any potential confusion.

Another option (that hopefully doesn't require too much work) is to just have two versions: one for each parameterization. If manageable, I suppose it could be one and you just do the conversion between them internally. I'd still personally just do the former and be done with it, but if you can't decide, that's probably a good sign that both are needed. Besides, many function generators let you parameterize both ways anyhow.

@zakv
Copy link
Contributor Author

zakv commented Nov 10, 2021

Sounds like that's two votes for amplitude/offset rather than level_0/level_1! I think the two-method option seems reasonable given that function generators have that behavior and a similar thing was done with exp_ramp() and exp_ramp_t(). Also I've already set it up with level_0/level_1 so leaving that in isn't much work.

I don't have a function generator handy so I think it'd be good to double check the expected behavior. Suppose the amplitude is 0.5 and the offset is 1. In that case, what would the high/low levels be? And should it start low or start high? And if the duty cycle is 0.1 it should be in the low state 90% of the time and in the high state 10% of the time (assuming amplitude is set to a positive value) right? And how should negative amplitudes be handled?

@dihm
Copy link
Contributor

dihm commented Nov 10, 2021

Here is an example screen from a Tektronix AFG that I clipped from the manual.

image

It defines the amplitude as the peak-to-peak value, the offset as the DC component. So 0.5 and 1 would give a high/low of 1.25/0.75.

As for start, I'd say start high since you typically code the start for when something actually happens. That can be tuned by adjusting the phase parameter (default being zero, starting high at time zero). So if you wanted it to start low, you could just set phase to pi. I think it is convention to define duty cycle as percentage on, so 10% duty cycle is 10% on, 90% off.

Personally, I think it should enforce positive amplitude and leave setting negative values to appropriately setting the offset.

Finally, this may be a bit too much, but you could probably add a debug kwarg that prints the equivalent high/low values. That should help users ensure what they have programmed is what they actually want without having to fire up runviewer.

@dihm
Copy link
Contributor

dihm commented Nov 10, 2021

Actually, looking at that screen clip and thinking a moment, maybe a delay setting instead of a phase would be easier to parse for a square wave?

…then added square_wave_levels() method which uses the old parameterization.

This PR also inverts the previous meaning of duty cycle in the old parameterization. Now duty cycle is the fraction of the time spent outputing `level_0` rather than `level_1` in square_wave_levels().
@zakv
Copy link
Contributor Author

zakv commented Nov 10, 2021

Thanks for the suggestions! I've updated the code to have to parameterizations: square_wave() which uses amplitude/offset and square_wave_levels() which uses level_0/level_1. I agree with your point that square_wave() should start HIGH when the initial phase is set to zero, so it does so.

For now no error is raised if amplitude is set to a negative value since that is probably an easy/convenient hack for starting LOW rather than HIGH (although you can do the same thing by adjusting the phase, you have to think for ~30 seconds about whether you need to set phase = duty_cycle or phase = 1 - duty_cycle).

Currently the methods are setup with a phase argument rather than a delay argument, mainly just because that's how I had already set it up. I think specifying the phase is maybe marginally more convenient. For example if you use the delay argument to start LOW instead of HIGH, then you have to calculate the required delay from the frequency. It's not hard to convert between phase and delay though so I think either would be reasonable.

Copy link
Contributor

@dihm dihm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Merging.

Thanks Zak!

@dihm dihm merged commit 4d26344 into labscript-suite:master Nov 10, 2021
@zakv zakv deleted the square-wave branch November 11, 2021 00:26
dihm added a commit that referenced this pull request Dec 7, 2021
commit f6409e0
Author: David Meyer <[email protected]>
Date:   Tue Dec 7 10:11:56 2021 -0500

    Update setup.cfg to show python 3.9 support.

commit 4d26344
Merge: 62455b5 deca5d8
Author: David Meyer <[email protected]>
Date:   Wed Nov 10 18:42:45 2021 -0500

    Merge pull request #77 from zakv/square-wave

    Added AnalogQuantity.square_wave().

commit deca5d8
Author: Zak V <[email protected]>
Date:   Wed Nov 10 18:00:24 2021 -0500

    Changed the parameterization of AnalogQuantity.square_wave() inputs, then added square_wave_levels() method which uses the old parameterization.

    This PR also inverts the previous meaning of duty cycle in the old parameterization. Now duty cycle is the fraction of the time spent outputing `level_0` rather than `level_1` in square_wave_levels().

commit 62455b5
Merge: 13239cc bda942a
Author: David Meyer <[email protected]>
Date:   Wed Nov 10 08:53:04 2021 -0500

    Merge pull request #84 from chrisjbillington/full-unitconversion-import-path

    Save the full import path of unit conversion classes

commit bda942a
Author: chrisjbillington <[email protected]>
Date:   Tue Nov 9 16:50:14 2021 +1100

    Save the full import path of unit conversion classes

    This allows unit conversion classes located outside of `labscript_utils`
    to be used.

    Closes #71

commit 13239cc
Merge: 83e10e5 4716ceb
Author: zakv <[email protected]>
Date:   Fri Aug 20 16:48:15 2021 -0400

    Merge pull request #72 from zakv/save-git-info

    Save git info

commit 83e10e5
Merge: 9fbd24c ea17d77
Author: zakv <[email protected]>
Date:   Fri Aug 20 16:47:49 2021 -0400

    Merge pull request #73 from philipstarkey/feature/performance-improvements

    Performance improvements

commit 9fbd24c
Merge: 04db899 736cb32
Author: zakv <[email protected]>
Date:   Fri Aug 20 08:33:57 2021 -0400

    Merge pull request #79 from zakv/fix-78

    Fix #78: labscript.py accidentally overwrites dedent()

commit 04db899
Merge: a805b69 9915e50
Author: David Meyer <[email protected]>
Date:   Fri Jul 16 14:54:58 2021 -0400

    Merge pull request #74 from dihm/labscript-docs

    Initial pass at API docs for labscript

commit 9915e50
Author: David Meyer <[email protected]>
Date:   Thu Jul 15 10:17:58 2021 -0400

    Even more docstring coverage in the API.

commit a4057e5
Author: David Meyer <[email protected]>
Date:   Wed Jul 14 10:38:13 2021 -0400

    Adding more docstrings to the API.

commit 846377d
Author: David Meyer <[email protected]>
Date:   Wed Jul 14 10:34:02 2021 -0400

    Remove colorama stuff. Doesn't work on RTD anyway. Now everything will
    be more consistent.

commit 073ceb4
Author: David Meyer <[email protected]>
Date:   Wed Jul 14 10:08:19 2021 -0400

    Add missing dependency that sphinx should bring in automatically but doesn't on RTD.

commit 08071b1
Author: David Meyer <[email protected]>
Date:   Wed Jul 14 09:51:36 2021 -0400

    Update sphinx pin to newest stable version.

commit 4b6f805
Author: David Meyer <[email protected]>
Date:   Wed Jul 14 09:41:26 2021 -0400

    Quick hack to get a coverage percentage of the API documentation.

    We can't easily use the coverage extension because it counts things
    documented via the `undoc-members` flag as documented!

    Actual items that need documenting can also be printed if desired by
    setting the `undoc_print_objects` flag in conf.py.
    This is automatically done for RTD builds.

commit 09091e3
Author: David Meyer <[email protected]>
Date:   Tue Jul 13 18:02:21 2021 -0400

    Add docstrings for all of the defined functions.

commit c1960c0
Author: David Meyer <[email protected]>
Date:   Fri Jul 9 11:45:36 2021 -0400

    Clean up warning messages and dead code.

commit b9be2c3
Author: David Meyer <[email protected]>
Date:   Fri Jul 9 11:23:00 2021 -0400

    Version bump RTD theme.

commit 7e086b6
Author: David Meyer <[email protected]>
Date:   Fri Apr 2 13:23:33 2021 -0400

    Version bump sphinx build requirement to 3.2.1

commit 689bc40
Author: David Meyer <[email protected]>
Date:   Fri Apr 2 12:19:55 2021 -0400

    Adding the connection diagram figure from Phil's thesis.

commit f5b8eed
Author: David Meyer <[email protected]>
Date:   Tue Nov 17 07:29:34 2020 -0500

    Version bump sphinx to 3.1.2

    This fixes issue where autodoc does not correctly handle decorated function
    signatures.

commit 6512ac6
Author: David Meyer <[email protected]>
Date:   Mon Nov 16 20:17:19 2020 -0500

    Version bump sphinx

    Fixes issue with using autosummary templates

commit 0e91b09
Author: David Meyer <[email protected]>
Date:   Mon Nov 16 18:39:30 2020 -0500

    Initial pass at API docs for labscript.

    This relies on recursive features and custom templates for autosummary,
    as described in https://stackoverflow.com/questions/2701998/sphinx-autodoc-is-not-automatic-enough/62613202#62613202

commit 736cb32
Author: Zak V <[email protected]>
Date:   Thu Feb 25 19:23:29 2021 -0500

    Fixed issue 78 in which labscript.py accidentally overwrote labscript_utils.dedent() with matplotlib.cbook.dedent().

commit 054e281
Author: Zak V <[email protected]>
Date:   Thu Feb 25 18:40:41 2021 -0500

    Added AnalogQuantity.square_wave().

commit 4716ceb
Author: Zak V <[email protected]>
Date:   Thu Oct 15 05:47:31 2020 -0400

    Reduced _vcs_cache_rlock contention in save_labscripts().

commit ea17d77
Author: Phil Starkey <[email protected]>
Date:   Thu Oct 15 18:50:45 2020 +1100

    Performance improvements

    These small changes seem to improve compilation time significantly. Tests (done with saving of hg info disabled) indicate compilation time for a complex experiment is decreased somewhere between 25-33%.

commit 903cc43
Author: Zak V <[email protected]>
Date:   Wed Oct 14 03:59:16 2020 -0400

    Worked on some thread safety issues with vcs caching.

commit 0f3f187
Author: Zak V <[email protected]>
Date:   Tue Oct 13 02:01:49 2020 -0400

    Results from git/hg are now cached for faster shot compilation.

commit 6022d97
Author: Zak V <[email protected]>
Date:   Sun Oct 4 12:52:10 2020 -0400

    Added '--tags' to call to git describe.

commit 2025755
Author: Zak V <[email protected]>
Date:   Sun Oct 4 12:38:10 2020 -0400

    Updated git/hg error message.

commit dc8d917
Author: Zak V <[email protected]>
Date:   Sun Oct 4 12:35:39 2020 -0400

    Added "git describe --always HEAD" to git commands.

commit 9a20221
Author: Zak V <[email protected]>
Date:   Sun Oct 4 12:30:44 2020 -0400

    Removed "--verify" from git rev-parse call.

commit 58ed6db
Author: Zak V <[email protected]>
Date:   Sun Oct 4 12:28:35 2020 -0400

    save_labscripts()'s calls to git/hg for a given file now run in parallel.

commit df4e17f
Author: Zak V <[email protected]>
Date:   Sat Oct 3 14:35:59 2020 -0400

    Added support for save_hg_info and save_git_info options in the labconfig [labscript] section.

commit e0073ea
Author: Zak V <[email protected]>
Date:   Sat Oct 3 13:48:15 2020 -0400

    Added support for saving git repo info.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants