Fourier Series Expansion G4PTool 1.1.0, Processing 1.5.1, and G4P 1.7.6
in
Share your Work
•
10 months ago
The following screen shots are from sketch that shows a finite number of Fourier series sums. Originally the sketch had no gui and produced sums continuously. I then used the G4PTool 1.1.0, Processing 1.5.1, and G4P 1.7.6 and was then able to add a text box, slider control and link these to code in my sketch. Having completed use of the Tool, the sketch ran perfectly on Processing 1.5.1 and Processing 2.0b6 both using G4P 1.7.6.
Just paste the following sketch into Processing, hit run and move the slider control to show a varying number of sums:
import guicomponents.*;
int t=0;
int k=1;
int n=1;
float f=.0025;
int height=200;
int width=1000;
float[] x = new float[width+1];
float[] y = new float[width+1];
float[] y_sum = new float[width+1];
void setup()
{
size(800, 200);
stroke(0, 0, 0);
createGUI();
}
void draw()
{
zero_arrays();
for (k=1; k<=n; k++) {
background(122, 232, 90);
fourier();
sum();
plot();
}
}
int t1=0;
void delay()
{
t1=millis();
while (millis()-t1 < 1){
}
}
/*
Using Fourier expansion with cycle frequency f over time t,
we can write an ideal square wave as an infinite series of
the form: x(t)=[k=1..00] (4/pi)(sin(2pi(2k-1)ft))/(2k-1)
*/
void fourier()
{
for (t=0; t<width; t++) {
y[t]=(2/PI)*(sin(2*PI*(2*k-1)*f*t))/(2*k-1);
x[t]=t;
}
return;
}
/* =========================================================
* ==== WARNING ===
* =========================================================
* The code in this tab has been generated from the GUI form
* designer and care should be taken when editing this file.
* Only add/edit code inside the event handlers i.e. only
* use lines between the matching comment tags. e.g.
void myBtnEvents(GButton button) { //_CODE_:button1:12356:
// It is safe to enter your event code here
} //_CODE_:button1:12356:
* Do not rename this tab!
* =========================================================
*/
void slider14_Change1(GHorzSlider horzslider) { //_CODE_:slider14:575342:
textfield1.setText(str(slider14.getValue()));
n=slider14.getValue();
} //_CODE_:slider14:575342:
void textfield1_Enter1(GTextField textfield) { //_CODE_:textfield1:374665:
} //_CODE_:textfield1:374665:
// Create all the GUI controls.
// autogenerated do not edit
void createGUI(){
G4P.setColorScheme(this, GCScheme.BLUE_SCHEME);
G4P.messagesEnabled(false);
slider14 = new GHorzSlider(this, 15, 58, 100, 12);
slider14.setLimits(1, 1, 25);
slider14.addEventHandler(this, "slider14_Change1");
textfield1 = new GTextField(this, "1", 15, 31, 29, 20, false);
textfield1.addEventHandler(this, "textfield1_Enter1");
label1 = new GLabel(this, "Fourier Series Expansion", 335, 4, 165, 20);
label2 = new GLabel(this, "number of terms in series", 15, 5, 80, 20);
}
// Variable declarations
// autogenerated do not edit
GHorzSlider slider14;
GTextField textfield1;
GLabel label1;
GLabel label2;
void plot()
{
for (int t = 0; t < width-1; t++) {
line(x[t], (height/2)+(height/2)*y_sum[t], x[t+1], (height/2)+(height/2)*y_sum[t+1]);
//point(x[t], (height/2)+(height/2)*y[t]);
}
}
void sum()
{
for (t=0; t<width; t++) {
y_sum[t]=y_sum[t]+y[t];
}
return;
}
/*
Zero arrays x[t] and y[t].
*/
void zero_arrays()
{
for (t=0; t<width; t++) {
y[t]=0;
y_sum[t]=0;
x[t]=0;
}
return;
}
1