Creating a SquareWave

Dear All,

 

I want to create a square wave where the step is number of pulses I wanna see, unitless, the pulseinter is the is the tickness of the pulse, it's unit is time, and the delay is the time in between the end and the start of the pulse, again the unit is the time. I wonder if you could help me to understand why my function is not working.

function squarewave(k2400, step, maxamp, minamp, pulseinter, delay, direction)
    variable k2400, step, maxamp, minamp, pulseinter, delay, direction
    variable sqwave, deltaamp, t0, elapsedTime, phase
    variable i

    // Calculate the amplitude change per step
    deltaamp = (maxamp - minamp) / step
    phase = pulseinter+delay
    // Start the timer and get the initial time
    t0 = stopMSTimer(-2)
   do
    elapsedTime= stopMSTimer(-2)-t0
        for(i=0; elapsedTime/i<=step; i=floor((elapsedTime+delay))/phase)
            for(elapsedTime=delay; elapsedTime+pulseinter-i*phase<=0; sqwave=i*deltaamp)
            sqwave=sqwave
            print "elapsedTime:", elapsedTime, "i:", Round((elapsedTime+delay)/phase)-1, "sqwave:", sqwave
        endfor
            for(elapsedTime=0; elapsedTime+pulseinter-i*phase>0; sqwave=0)
            sqwave=sqwave
            print "elapsedTime:", elapsedTime, "i:", Round((elapsedTime+delay)/phase)-1, "sqwave:", sqwave
        endfor
        endfor
   while(elapsedTime<=phase*step)
end

 

P.S. Don't worry about the variable k2400 and the direction, they are for another use.

Can you elaborate on what you want to achieve and what you expect from the function?

Apparently you want to create a wave, but the function doesn't do that, neither does it return anything that could be assigned to a wave element. 

 

 

EDIT:

Maybe you can adjust the following to your needs, assuming that I interpreted your question halfway correctly:

function SquareWave(variable length, variable amplitude, variable width)
    Make/O/N=(Length) w
    w = mod(p, width) < width/2 ? amplitude : -amplitude
end

 

 

Thank you! Problem has been solved and here it is with a couple of personal usage parts:

#pragma rtGlobals=1     // Use modern global access method.
#include <colorwaveeditor>
// Elbek Javokhir Keskinoglu
// 13/08/24
// SOT Measurement

function squwave(k2400, sr830, maxamp, minamp, pulseinter, delay, step,  bg, set)
    variable k2400, sr830, maxamp, minamp, pulseinter, delay, step,  bg, set
    variable sqwave, deltaamp, t0, elapsedTime, phase, i, howlong
    variable row
    wave w_sqwave, w_i, w_elapsedTime, w_readvoltage, w_lockin

    // Create waves to store the data with initial size
    make /N=(0) /O w_sqwave, w_i, w_elapsedTime, w_readvoltage, w_lockin

    // Start the timer and get the initial time
    t0 = stopMSTimer(-2)

    // Calculate the amplitude change per step
    phase = pulseinter + delay
    howlong = phase * step
    deltaamp = (maxamp - minamp) / step
    setamplitude(sr830, bg)
    // Initialize the current
    setk2400current(k2400, 0)
    row = 0
    do
        elapsedTime = stopMSTimer(-2) - t0

        if (elapsedTime - phase >= howlong)
            break
        endif

        i = floor(elapsedTime / phase)

        if (mod(elapsedTime, phase) <= pulseinter)
            sqwave = minamp + i * deltaamp
        else
            sqwave = 1e-100
        endif

        if ((phase*0.45) < mod(elapsedTime, phase) && mod(elapsedTime, phase) < (phase*0.55))
            setamplitude(sr830, set)
        else
            setamplitude(sr830, bg)
        endif

        // Set the current
        setk2400current(k2400, sqwave)

        // Append data to the waves
        redimension /N=(row+1) w_sqwave, w_i, w_elapsedTime, w_readvoltage, w_lockin
        w_sqwave[row] = sqwave
        w_i[row] = i
        w_elapsedTime[row] = elapsedTime
        w_lockin[row] = getAmplitude(sr830)
        if (row > 0)  // Apply the shift for all rows except the first
            w_readvoltage[row - 1] = readvoltage(k2400)
        endif

        // Print the values for debugging
        print sqwave, i, elapsedTime, w_readvoltage[row], w_lockin[row]

        row += 1
        //do1d("time", t0, howlong, howlong, 0)
    while (1)

   
    // Append data to a table
    string tableName = "SquareWaveTable"
    edit /N=$tableName w_sqwave, w_i, w_elapsedTime, w_readvoltage, w_lockin
    Display w_readvoltage,w_lockin vs w_elapsedTime
     TextBox/C/N=text0/A=MC "K2450, SR83 vs Time"
    // Save the table to a CSV file in the same folder as the current experiment
    string fileName = GetDataFolder(1) + "SquareWaveData.csv"
    SaveTableCopy /I/M="\r\n"/N=1/O/P=fileName /T=2 /W=$tableName

end