PlayWaveAdd scaling question

Discussions and questions about the script language
tlstay
Posts: 13
Joined: 04 Oct 2017, 20:59
Contact:

PlayWaveAdd scaling question

Unread postby tlstay » 13 May 2022, 23:20

Hello, my question is, when I'm adding a wave from an existing Spike2 data file using PlayWaveAdd, how can I change the amplitude of the voltage being sent out by the DAC? Currently, no matter how I resize the wave before adding it to the online wave list, it is giving me the same output, which happens to go beyond the voltage limits of my controlled motor.

For more details, I'm creating a sum-of-sines stimulus vector in Matlab, exporting it as a .csv file, and then importing that into Spike2 and saving it as a .smrx file. I then add that .smrx file into my desired run using PlayWaveAdd, using the command variant where I specify the path to the .smrx file. The issue is that no matter what the absolute values are that I save in Matlab, it appears that Spike2 always interprets them to span the whole +-5V range of our 1401. This is also true regardless of what units I specify during import (V, mV, etc); an input wave that goes from -1 to 1 gives the same DAC output as a wave that goes from -0.00001 to 0.00001 (checked on an oscilloscope). This is an undesired behavior, since I want to run a motor a certain length of time before switching directions, and if the overall wave covers the full -5 to 5V range, the motor always ends up hitting a hard stop.

So, is there a way to specify the amplitude of the wave that gets added using PlayWaveAdd? Or, is there something about the function that I'm not understanding correctly? Thank you for any suggestions.

User avatar
Tim Bergel
Site Admin
Posts: 2379
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: PlayWaveAdd scaling question

Unread postby Tim Bergel » 14 May 2022, 10:46

The scaling of output waveforms is a complicated topic, made more complicated by your importing this data.

If you consider a waveform sampled with the 1401, that waveform data is 16-bit integers with the entire 16-bit range covering the ADC input range. When the waveform is output to a DAC the 16-bit integer values are simply played-out through the DAC. As long as the voltage range of the ADC and DACs are the same the result is an output which exactly matches the input sampled by the 1401.

When importing data from a text file, the values read in are converted to 16-bit integers in such a way that the range of values encountered in the text exactly covers the entire 16-bit range. So when that data is replayed, the output waveforms cover the entire range of the DAC.

I can think of several ways to approach this. The first would be to put some dummy data at the start or end of the CSV file, this data being the values which you want to correspond to the DAC limits. So if you had data values running from -1 to +1 and you wanted these to be interpreted as volts, you could add 5 and -5 values to the end which would force the import mechanism to generate 16-bit data covering this +5 to -5 range and your -1 to +1 values would be 1/5 of the overall range. The 5 and -5 values can be left out of the actual waveform which you use.

Alternatively, you could use a script - read your imported data into an array, scale this array with ArrMul() and then add this array into the sampling configuration. Array data added in this way is scaled to integers taking +5 as the maximum and -5 as the minimum DAC output. Alternatively you can work with integer data and generate the required integer values directly.

I believe that data from a virtual channel (where there is no underlying 16-bit data) is also handled so that the DAC range is covered by data values from -5 to +5, though I am not sure about this. So if you created a virtual channel containing a suitably scaled (by simple multiplication in the virtual channel expression such as "ch(1) * 0.5") version of your imported data and added that, you should be able to manipulate the output levels by adjusting scaling. This is the easiest method I think so I suggest you try this first, but I am not sure if it will actually work. You may need to convert the waveform from being specified as channel x in file y to data stored within the sampling configuration for this method to work.

I hope that helps, both to explain what is going on and to allow you to get the outputs which you want. Do feel free to get back to me if you need more advice.
Tim Bergel Cambridge Electronic Design

tlstay
Posts: 13
Joined: 04 Oct 2017, 20:59
Contact:

Re: PlayWaveAdd scaling question

Unread postby tlstay » 09 Jun 2022, 18:43

Thank you for the response. I ended up using your first method, adding two dummy data points at the end of the file to define the full range I wanted (and thereby scale down the working input) and then simply defining the time range in PlayWaveAdd to exclude those two points. Thanks again.


Return to “Script language”

Who is online

Users browsing this forum: No registered users and 1 guest

cron