Forum Archive

Problem in Pythonista

louislbc

Hi all,

    I encountered a problem in using Pythonista Scene to draw a flicker. The frequency of flicker that I want to set is 15.2 Hz. I try to realize it by a sinusoidal sampling method (see below code). This method has proven to be effective to render a flicker with a precise frequency in other platform such as MATLAB and Unity 3D. I found the rendering is precise in generating a flicker with 13 Hz, 14 Hz , 15Hz or any frequency that is an integer, such as 15.2 Hz, 15.4 Hz etc. However,  to draw a flicker whose frequency is not an integer, the rendering is not precise. The code I used to draw a flicker is below.

from scene import *
from math import sin, pi

class MyScene (Scene):

def update(self):
    translate(self.size.w/2, self.size.h/2)
    freq = 15.2
    global COUNT
    weight = (1+sin(2*pi*freq*COUNT/60))/2
    COUNT += 1
    fill((weight,weight,weight))
    rect(-80, -80, 160, 160)
    if COUNT == 60:
        COUNT = 0

COUNT = 0
run(MyScene(),show_fps=True,frame_interval=1)

The code was run in several ipad and iphone. But the problem is the same.
Does anyone know how to make the flicker precise? Thank you!

enceladus

Use the time rather than the count

import scene
from math import sin, pi

class MyScene (scene.Scene):
    def update(self):
        scene.translate(self.size.w/2, self.size.h/2)
        freq = 15.2
        weight = (1+sin(2*pi*freq*self.t))/2
        scene.fill((weight,weight,weight))
        scene.rect(-80, -80, 160, 160)

scene.run(MyScene(),show_fps=True,frame_interval=1)