Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
July 7th, 2013, 10:49 AM
 mpc2012
Registered User

Join Date: Jul 2013
Posts: 1
Time spent in forums: 20 m 22 sec
Reputation 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
July 7th, 2013, 09:47 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,170
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 27 m 9 sec
Reputation Power: 455
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!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Sine wave frequency Sweep