Announcement

Collapse
No announcement yet.

Need an function for triangle wave en square wave

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rod Macia
    replied
    For Square wave you can probably get a good approximation with

    x = a* sgn(sin(t))

    a = amplitude
    t = time

    of course you have to scale "a" and "t" to fit your chosen display Grid

    sgn(sin(t))
    will be 1 when the sinusoid is positive, −1 when the sinusoid is negative, and 0 at the discontinuities.

    Rod

    Leave a comment:


  • Paul Dixon
    replied
    No guarantee of any kind but maybe this will help:
    Code:
    'PBCC5.0 program
    #BREAK ON
    #COMPILE EXE
    #DIM ALL
     
    FUNCTION Square(frequency AS EXT, time AS EXT, amplitude AS EXT, xoffset AS EXT, yoffset AS EXT) AS EXT
    'frequency is in Hertz
    'time and xoffset are in seconds
    
    LOCAL temp AS EXT
    
    temp = (2* (time + xoffset) * frequency) MOD 2
    
        SELECT CASE temp
            CASE < 1
                Square =  amplitude  + yoffset
    
            CASE ELSE
                Square =  0 + yoffset
    
        END SELECT
    
    END FUNCTION
    
         
    FUNCTION Triangle(frequency AS EXT, time AS EXT, amplitude AS EXT, xoffset AS EXT, yoffset AS EXT) AS EXT
    'frequency is in Hertz
    'time and xoffset are in seconds
    
    LOCAL temp AS EXT
    
    temp = (2* (time + xoffset) * frequency) MOD 2
       
        SELECT CASE temp
            CASE < 1
                Triangle =  (amplitude * temp) MOD amplitude  + yoffset
                
            CASE ELSE
                Triangle =  amplitude - (amplitude * temp) MOD amplitude + yoffset
                
        END SELECT
    
    END FUNCTION
    
           
    FUNCTION PBMAIN () AS LONG
    LOCAL time AS EXT
    LOCAL hWin AS DWORD
    LOCAL SquareWaveFreq, TriangleWaveFreq AS EXT
    LOCAL SquareWaveAmplitude, TriangleWaveAmplitude AS EXT
    LOCAL SquareWaveXOffset, TriangleWaveXOffset AS EXT
    LOCAL SquareWaveYOffset, TriangleWaveYOffset AS EXT
    
    
    SquareWaveFreq =         10     '10 Hertz
    TriangleWaveFreq =       15     '15 Hertz
    SquareWaveAmplitude =   100
    TriangleWaveAmplitude = 150
    SquareWaveXOffset =       0     '0 seconds shift
    TriangleWaveXOffset =     0.075 '0.075 seconds shift
    SquareWaveYOffset =     100
    TriangleWaveYOffset =   300
    
    GRAPHIC WINDOW "Triangle and square", 10,10,1100, 500 TO hWin
    GRAPHIC ATTACH hWin, 0
    GRAPHIC CLEAR %BLACK
    
    FOR time = 0 TO 1 STEP 0.001
        GRAPHIC SET PIXEL (1000*time,Square(SquareWaveFreq,time,SquareWaveAmplitude,SquareWaveXOffset,SquareWaveYOffset)) ,%RED
        GRAPHIC SET PIXEL (1000*time,Triangle(TriangleWaveFreq,time,TriangleWaveAmplitude,TriangleWaveXOffset,TriangleWaveYOffset) ) ,%GREEN
    NEXT
      
    GRAPHIC DETACH
    WAITKEY$
    END FUNCTION

    Leave a comment:


  • Chris Holbrook
    replied
    Here's the example from Jose Roca modified to show a sawtooth. By comparing the two you will be able to see where you need to make changes.

    Code:
    ' ========================================================================================
    ' Jose Roca's sinewave example crudely modified to show a sawtooth waveform
    ' Chris Holbrook Sep 2008
    ' ========================================================================================
    
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
    
    ' ========================================================================================
    ' Main
    ' ========================================================================================
    FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, _
                      BYVAL pszCmdLine AS ASCIIZ PTR, BYVAL iCmdShow AS LONG) AS LONG
    
       LOCAL szAppName AS ASCIIZ * 256
       LOCAL msg       AS tagMsg
       LOCAL hWnd      AS DWORD
       LOCAL wc        AS WNDCLASS
       LOCAL szCaption AS ASCIIZ * 256
    
       szAppName        = "Sawtooth"
       wc.style         = %CS_HREDRAW OR %CS_VREDRAW
       wc.lpfnWndProc   = CODEPTR(WndProc)
       wc.cbClsExtra    = 0
       wc.cbWndExtra    = 0
       wc.hInstance     = hInstance
       wc.hIcon         = LoadIcon(%NULL, BYVAL %IDI_APPLICATION)
       wc.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
       wc.hbrBackground = GetStockObject(%WHITE_BRUSH)
       wc.lpszMenuName  = %NULL
       wc.lpszClassName = VARPTR(szAppName)
    
       IF ISFALSE RegisterClass(wc) THEN
          FUNCTION = %TRUE
          EXIT FUNCTION
       END IF
    
       szCaption = "Sawtooth Using Polyline"
       hWnd = CreateWindow (szAppName, _             ' window class name
                            szCaption, _             ' window caption
                            %WS_OVERLAPPEDWINDOW, _  ' window style
                            %CW_USEDEFAULT, _        ' initial x position
                            %CW_USEDEFAULT, _        ' initial y position
                            %CW_USEDEFAULT, _        ' initial x size
                            %CW_USEDEFAULT, _        ' initial y size
                            %NULL, _                 ' parent window handle
                            %NULL, _                 ' window menu handle
                            hInstance, _             ' program instance handle
                            %NULL)                   ' creation parameters
    
       ShowWindow hWnd, iCmdShow
       UpdateWindow hWnd
    
       WHILE GetMessage(msg, %NULL, 0, 0)
          TranslateMessage msg
          DispatchMessage msg
       WEND
    
       FUNCTION = Msg.wParam
    
    END FUNCTION
    ' ========================================================================================
    
    ' ========================================================================================
    ' Main dialog callback.
    ' ========================================================================================
    FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL message AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
    
       STATIC cxClient AS LONG
       STATIC cyClient AS LONG
       LOCAL  hdc AS DWORD
       LOCAL  i AS LONG
       LOCAL  ps  AS PAINTSTRUCT
       LOCAL  NUM AS LONG
       DIM    apt(999) AS POINTAPI
    
       SELECT CASE message
    
          CASE %WM_SIZE
             cxClient = LOWRD(lParam)
             cyClient = HIWRD(lParam)
             FUNCTION = 0
             EXIT FUNCTION
    
          CASE %WM_PAINT
             hdc = BeginPaint(hwnd, ps)
             MoveToEx hdc, 0, cyClient / 2, BYVAL %NULL
             LineTo hdc, cxClient, cyClient / 2
             FOR i = LBOUND(apt) TO UBOUND(apt)
                apt(i).x = i * cxClient / UBOUND(apt)
                apt(i).y = i*(cyClient/2*UBOUND(apt)) MOD 250
             NEXT
             POLYLINE hdc, apt(0), UBOUND(apt)
             EndPaint hWnd, ps
             FUNCTION = 0
             EXIT FUNCTION
    
         CASE %WM_DESTROY
             PostQuitMessage 0
             FUNCTION = 0
             EXIT FUNCTION
    
       END SELECT
    
       FUNCTION = DefWindowProc(hWnd, message, wParam, lParam)
    
    END FUNCTION
    ' ========================================================================================

    Leave a comment:


  • Stephane Fonteyne
    replied
    Example

    Chris

    Can you give an PBWin example how the user can custimize the square wave and triangle wave

    Ik read your documentation but I don't understand how I can create an function this functions

    In mathematics is the function of the linear function:

    y = m * x + q

    rico = dy/dx

    That al what I have known

    Please can you help me with an PBWin example of this two waves

    Kind regards
    Stephane

    Leave a comment:


  • Chris Holbrook
    replied
    Answers is the place to go to get the answers you need and to ask the questions you want

    Leave a comment:


  • Stephane Fonteyne
    replied
    Hello

    Can you please give me an example for build an triangle function?

    Thanks
    Stephane

    Leave a comment:


  • Chris Holbrook
    replied
    here's an example of a sine wave which you could adapt.
    http://www.jose.it-berater.org/smffo...php?topic=69.0

    Leave a comment:


  • Arie Verheul
    replied
    Piecewise linear waveforms

    Although rectangle and triangle waveforms can be approximated with Fourier synthesis, in real electronics these waveforms do not result from functions but are made with switches.

    To produce a picture of any piecewise linear waveform on the screen simply use:

    GRAPHIC LINE - STEP (x2!, y2!), rgbColor&

    Just write one period and make it repeat.

    Arie Verheul

    Leave a comment:


  • Need an function for triangle wave en square wave

    Dear programmers

    I would like programming an animate oscilloscope in PBWin with the Graphic functions but I don't find an function for create an customize function for generate an triangle wave and sqaure wave.

    Can someone help me for written the function?
    I don't know how I can written this two functions

    With kind regards
    Stephane

    I love PowerBASIC !
Working...
X
😀
🥰
🤢
😎
😡
👍
👎