Exemple #1
0
func mergeChannels(in1 <-chan []complex128, in2 <-chan []complex128) chan []complex128 {
	out := make(chan []complex128)
	go func() {
		fmt.Printf("Writing...\n")
		for cIn1 := range in1 {
			cIn2 := <-in2
			if len(cIn1) != len(cIn2) {
				panic("oops, columns don't match... :(")
			}
			cOut := make([]complex128, len(cIn1), len(cIn1))
			for i := range cIn1 {
				power1, angle1 := cmplx.Polar(cIn1[i])
				power2, angle2 := cmplx.Polar(cIn2[i])
				cOut[i] = cmplx.Rect(power1, angle1)
				if i > 48 && i <= 72 {
					cOut[i] = 0
				}
				// HACK variable to stop go complaining about unused variables :(
				cIn2[i] = cmplx.Rect(power2, angle2)
			}
			out <- cOut
		}
		close(out)
	}()
	return out
}
Exemple #2
0
func TestSineOsc_expwdt_approx(t *testing.T) {
	const sampleRate = 96000

	var osc SineOsc
	Init(&osc, Params{sampleRate})
	for freq, err := range map[float64]float64{
		32:    0.00,
		64:    0.00,
		128:   0.00,
		256:   0.00,
		512:   0.00,
		1024:  0.00,
		2048:  0.00,
		4096:  0.01,
		8192:  0.02,
		16384: 0.09,
	} {
		_, θ := cmplx.Polar(osc.expwdt_approx(freq))
		actualFreq := θ / 2 / math.Pi * sampleRate
		actualErr := freq/actualFreq - 1
		if math.Abs(err-actualErr) > .01 {
			t.Errorf("freq=%5.f:, expected %.2f, got %.2f", freq, err, actualErr)
		}
	}
}
Exemple #3
0
func addPolar(a, b Polar) Polar {

	// Add polar coordinates (5,140) and (3,70)
	x := cmplx.Rect(a.radius, a.angle)
	y := cmplx.Rect(b.radius, b.angle)
	radius, angle := cmplx.Polar(x + y)
	return Polar{radius, angle}

}
Exemple #4
0
func NaiveSpec(c gospec.Context) {
	in := make([]complex128, 8)
	for i := range in {
		in[i] = complex(math.Cos(float64(i)/float64(len(in))*2*math.Pi), 0)
	}
	verify := func(out []complex128, start, stride int, name string) {
		c.Specify(name, func() {
			for i := start; i < len(out); i += stride {
				mag, ang := cmplx.Polar(out[i])
				if i == start+stride || i == len(out)+start-stride {
					c.Expect(mag, IsWithin(1e-9), float64(len(out)/stride)/2)
					if real(out[i]) < 0 {
						if ang < 0 {
							c.Expect(ang, IsWithin(1e-9), -math.Pi)
						} else {
							c.Expect(ang, IsWithin(1e-9), math.Pi)
						}
					} else {
						c.Expect(ang, IsWithin(1e-9), 0.0)
					}
				} else {
					c.Expect(mag, IsWithin(1e-9), 0.0)
				}
			}
		})
	}

	c.Specify("Test basic DFT", func() {
		out := make([]complex128, len(in))

		fft.DFT(in, out, 0, 1)
		verify(out, 0, 1, "Start/Stride 0/1")

		in2 := make([]complex128, 2*len(in))
		out2 := make([]complex128, 2*len(in))
		for i := range in2 {
			in2[i] = in[i/2]
		}
		fft.DFT(in2, out2, 0, 2)
		verify(out2, 0, 2, "Start/Stride 0/2")

		fft.DFT(in2, out2, 1, 2)
		verify(out2, 1, 2, "Start/Stride 1/2")

		in5 := make([]complex128, 5*len(in))
		out5 := make([]complex128, 5*len(in))
		for i := range in5 {
			in5[i] = in[i/5]
		}
		for i := 0; i < 5; i++ {
			fft.DFT(in5, out5, i, 5)
			verify(out5, i, 5, fmt.Sprintf("Start/Stride %d/%d", i, len(in5)/len(in)))
		}
	})
}
Exemple #5
0
func iteration(c complex128, iterations int) int {
	var z complex128
	for i := 0; i < iterations; i++ {
		z = mandelbrot(z, c)
		r, _ := cmplx.Polar(z)
		if r >= 2 {
			return i
		}
	}
	return 0
}
Exemple #6
0
func (ft *FT) encodeSpectrumComponent(i int, im *image.RGBA) {
	xo, yo := (ft.w-1)/2, (ft.h-1)/2
	for x := 0; x < ft.w; x++ {
		if cornered {
			xo, yo = 0, 0
		}
		for y := 0; y < ft.h; y++ {
			f := func(c complex128) (float64, float64, float64) {
				r, theta := cmplx.Polar(c)
				if r > 1.0 {
					r = math.Log(r) / math.Log(float64(ft.w*ft.h))
				} else {
					r = 0.0
				}
				theta = math.Mod(theta/(2.0*math.Pi)+1.0, 1.0)
				return HSLToRGB(theta, 1.0, r)
			}
			im.SetRGBA((x+xo)%ft.w+ft.w*i, (y+yo)%ft.h, ft.channels.LoadComplex(i, x, y, f))
		}
	}
}
Exemple #7
0
func clicking(c complex128, rot float64, biowl bool) game.Pos {
	var r, p float64
	var m uint16
	var pr, pf int8
	log.Println("RawClick:", c)
	c -= Center
	r, p = cmplx.Polar(c)
	p -= rot
	r -= InnerRadius
	if r < 0 {
		return game.Pos{-1, -1}
	}
	p = adowbiowl(p, biowl)
	m = uint16(r) / 35
	if m < 24 {
		pr = int8(m)
	} else {
		return game.Pos{127, 127}
	}
	pf = int8(p / OneFile)
	return game.Pos{pr, pf}
}
Exemple #8
0
// This example is adapted from Richard Lyon's "Understanding Digital Signal Processing," section 3.1.1.
func ExampleFFTReal() {
	numSamples := 8

	// Equation 3-10.
	x := func(n int) float64 {
		wave0 := math.Sin(2.0 * math.Pi * float64(n) / 8.0)
		wave1 := 0.5 * math.Sin(2*math.Pi*float64(n)/4.0+3.0*math.Pi/4.0)
		return wave0 + wave1
	}

	// Discretize our function by sampling at 8 points.
	a := make([]float64, numSamples)
	for i := 0; i < numSamples; i++ {
		a[i] = x(i)
	}

	X := FFTReal(a)

	// Print the magnitude and phase at each frequency.
	for i := 0; i < numSamples; i++ {
		r, θ := cmplx.Polar(X[i])
		θ *= 360.0 / (2 * math.Pi)
		if dsputils.Float64Equal(r, 0) {
			θ = 0 // (When the magnitude is close to 0, the angle is meaningless)
		}
		fmt.Printf("X(%d) = %.1f ∠ %.1f°\n", i, r, θ)
	}

	// Output:
	// X(0) = 0.0 ∠ 0.0°
	// X(1) = 4.0 ∠ -90.0°
	// X(2) = 2.0 ∠ 45.0°
	// X(3) = 0.0 ∠ 0.0°
	// X(4) = 0.0 ∠ 0.0°
	// X(5) = 0.0 ∠ 0.0°
	// X(6) = 2.0 ∠ -45.0°
	// X(7) = 4.0 ∠ 90.0°
}
Exemple #9
0
func (Polar) CognizeComplex(eye *be.Eye, v interface{}) {
	r, theta := cmplx.Polar(v.(complex128))
	eye.Show("Polar", New().Grow("R", r).Grow("Theta", theta))
}
Exemple #10
0
func ExamplePolar() {
	r, theta := cmplx.Polar(2i)
	fmt.Printf("r: %.1f, θ: %.1f*π", r, theta/math.Pi)
	// Output: r: 2.0, θ: 0.5*π
}
Exemple #11
0
func add180ToComplexNumner(complexNumber *complex128) {
	r, θ := cmplx.Polar(*complexNumber)
	*complexNumber = cmplx.Rect(r, θ+math.Pi/2)
}