#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Sine wave frequency Sweep


    Hi,

    I want to generate an sine wave where it changes the freq at a rate of 0.1hz to 10hz. I have my code like as below

    import math
    from math import pi,sin
    time_delta = 0

    for Freq in range(1,11):
    Freq*=0.1
    #print ("current freq", Freq)
    time_delta = 0
    while time_delta <= 10.0:
    w=2*pi*Freq
    y= sin(w*time_delta)+2
    time_delta = time_delta +0.1
    print y




    with this code I have problem where time delta and freq needs to be adjusted if I want to see the freq from 1 to 10hz.

    I want some robust so that I need to change the code for different frequencies.

    Suggest me the better code.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    What boundary conditions would you enforce when the frequency step changes? I'd think you'd want to enforce continuity as well as sign of slope. Phase, which you haven't used, is the only parameter to adjust. Or, if you also included amplitude you could match the slope as well. Meanwhile, you must keep track of the total time and plot time versus
    amplitude *sin(time*frequency + phase)
    to visualize the solution. I use gnuplot for graphics, although the graphics supplied along with scipy are exceedingly capable.
    Code:
    import math
    from math import pi,sin
    time_delta = 0
    
    for Freq in range(1,11):
        Freq*=0.1
        #print ("current freq", Freq)
        time_delta  =  0
        while time_delta <= 10.0:
            w=2*pi*Freq
            y= sin(w*time_delta)+2
            time_delta = time_delta +0.1
            print y
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo