Ejemplo n.º 1
0
// IDCTOrthogonal returns Inverse DCT (DCT-III) coefficients given a input
// signal.
func IDCTOrthogonal(dctCoef []float64) []float64 {
	n := len(dctCoef)
	nh := n / 2
	array := make([]complex128, n)

	theta := math.Pi / (2.0 * float64(n))

	c0 := math.Sqrt(1.0 / float64(n))
	c1 := math.Sqrt(2.0 / float64(n))

	// -1/4 Shift
	for k := 1; k < nh; k++ {
		w := cmplx.Exp(complex(0, float64(k)*theta))
		wCont := complex(imag(w), real(w))
		array[k] = w * complex(c1*dctCoef[k], 0)
		array[n-k] = wCont * complex(c1*dctCoef[n-k], 0)
	}
	array[0] = complex(c0*dctCoef[0], 0)
	array[nh] = complex(c1*dctCoef[nh], 0) *
		cmplx.Exp(complex(0, float64(nh)*theta))

	y := fft.IFFT(array)

	x := make([]float64, n)
	for i := 0; i < nh; i++ {
		x[2*i] = float64(n) * real(y[i])
		x[2*i+1] = float64(n) * real(y[n-1-i])
	}

	return x
}
Ejemplo n.º 2
0
// Rotation about the Z-axis.
func RotationZ(theta float64) *Gate {
	// R_z(theta) = cos(theta/2)I - i sin(theta/2)Z
	t := theta / 2
	nexp := cmplx.Exp(complex(0, -1*t))
	exp := cmplx.Exp(complex(0, t))
	return newOneQubitGate([4]complex128{
		nexp, 0,
		0, exp})
}
Ejemplo n.º 3
0
// Function to compute basis transformation matrix
func BasisTransform(th, phi float64) *[2][2]complex128 {

	BTMatrix := new([2][2]complex128)
	th_2, phi_2 := 0.5*th, 0.5*phi
	csn := cmplx.Cos(complex(th_2, 0.0)) * cmplx.Exp(complex(0.0, -1.0*phi_2))
	sn := cmplx.Sin(complex(th_2, 0.0)) * cmplx.Exp(complex(0.0, phi_2))

	BTMatrix[0][0] = cmplx.Conj(csn)
	BTMatrix[0][1] = cmplx.Conj(sn)
	BTMatrix[1][0] = complex(-1.0, 0.0) * sn
	BTMatrix[1][1] = csn

	return BTMatrix
}
Ejemplo n.º 4
0
func (self *ComplexV) Exp() *ComplexV {
	r := Zeros(len(*self))
	for k, a := range *self {
		(*r)[k] = cmplx.Exp(a)
	}
	return r
}
Ejemplo n.º 5
0
func fft(output []complex128, input []float64, stride int) {
	n := len(output)
	p := n / 2

	if n == 1 {
		if len(input) > 0 {
			output[0] = complex(input[0], 0)
		} else {
			output[0] = complex(0, 0)
		}
		return
	}

	var oddInput []float64
	if len(input) > stride {
		oddInput = input[stride:]
	}

	fft(output[:p], input, stride*2)
	fft(output[p:], oddInput, stride*2)

	for k, t := range output[:p] {
		a := complex(0, -2*math.Pi*float64(k)/float64(n))
		e := cmplx.Exp(a) * output[k+p]
		output[k] = t + e
		output[k+p] = t - e
	}
}
Ejemplo n.º 6
0
// CDF computes the value of the cumulative density function at x.
func (w Weibull) CDF(x float64) float64 {
	if x < 0 {
		return 0
	} else {
		return 1 - cmplx.Abs(cmplx.Exp(w.LogCDF(x)))
	}
}
Ejemplo n.º 7
0
// DCT returns DCT-II coefficients given an input signal.
func DCT(x []float64) []float64 {
	n := len(x)
	nh := n / 2

	evenVector := make([]float64, n)
	for i := 0; i < nh; i++ {
		evenVector[i], evenVector[n-1-i] = x[2*i], x[2*i+1]
	}

	array := fft.FFTReal(evenVector)
	theta := math.Pi / (2.0 * float64(n))

	// 1/4 Shift
	for k := 1; k < nh; k++ {
		w := cmplx.Exp(complex(0, -float64(k)*theta))
		wCont := -complex(imag(w), real(w))
		array[k] *= w
		array[n-k] *= wCont
	}
	array[nh] *= complex(math.Cos(theta*float64(nh)), 0.0)

	dctCoef := make([]float64, n)
	for i := range dctCoef {
		dctCoef[i] = real(array[i])
	}

	return dctCoef
}
Ejemplo n.º 8
0
// ReconstructSpectrum returns complex spectrum from amplitude
// and phase spectrum.
// angle(X(k)) and |X(k)| -> X(k)
func ReconstructSpectrum(amp, phase []float64) []complex128 {
	spec := make([]complex128, len(amp))
	for i := range amp {
		spec[i] = complex(amp[i], 0.0) * cmplx.Exp(complex(0.0, phase[i]))
	}
	return spec
}
Ejemplo n.º 9
0
func NewSDFT32(k, n int, window []float32) *SDFT32 {
	var win []complex64
	if len(window) == 0 {
		win = []complex64{complex(1, 0)}
	} else {
		win = make([]complex64, len(window))
		for i, w := range window {
			win[i] = complex(w, 0)
		}
	}
	s := &SDFT32{
		w: win,
		x: make([]complex64, n),
		e: make([]complex64, len(win)),
		s: make([]complex64, len(win)),
	}
	for i := 0; i < len(win); i++ {
		j := k - len(win)/2 + i
		if j < 0 {
			j += n
		} else if j >= n {
			j -= n
		}
		s.e[i] = complex64(cmplx.Exp(complex(0, 2*math.Pi*float64(j)/float64(n))))
	}
	return s
}
Ejemplo n.º 10
0
func modulateFSKComplex(bits []bool, sample_rate float64,
	zero_hz, one_hz, baud float64) []complex64 {
	const π = math.Pi
	Δt := 1.0 / sample_rate
	Δφ_one := 2 * π * one_hz * Δt
	Δφ_zero := 2 * π * zero_hz * Δt
	samples_per_bit := sample_rate / baud

	samples := make([]complex64, 0)
	φ := 0.0
	num_samples := 0.0
	for _, b := range bits {
		num_samples += samples_per_bit
		for ; num_samples > 0.5; num_samples -= 1.0 {
			if b {
				φ += Δφ_one
			} else {
				φ += Δφ_zero
			}
			samples = append(samples,
				complex64(cmplx.Exp(complex(0, φ))))
		}
	}
	return samples
}
Ejemplo n.º 11
0
// Convenience constructor for a qubit, specified by its spherical coordinates
// on the Bloch sphere.
func NewQubitWithBlochCoords(theta, phi float64) *QReg {
	// |psi> = cos(theta/2) + e^{i phi}sin(theta/2)
	t := complex(theta/2, 0)
	p := complex(phi, 0)
	qreg := &QReg{1, []complex128{cmplx.Cos(t),
		cmplx.Exp(complex(0, 1)*p) * cmplx.Sin(t)}}
	return qreg
}
Ejemplo n.º 12
0
// Function for calculating the self-energy matrices
func SelfEnergyEntries(currEnergy, modeEnergy, delta0, delta1, potential float64, t0 complex128, BT_Mat *[2][2]complex128) *[2][2]complex128 {
	s := new([2][2]complex128)

	SumEnergy := complex(currEnergy-modeEnergy+0.5*potential-delta0, utils.Zplus)
	SumEnergy /= complex(-2.0, 0.0) * t0
	SumEnergy += complex(1.0, 0.0)

	sig_uu := complex(-1.0, 0.0) * t0 * cmplx.Exp(1.0i*cmplx.Acos(SumEnergy))

	SumEnergy = complex(currEnergy-modeEnergy+0.5*potential-delta1, utils.Zplus)
	SumEnergy /= complex(-2.0, 0.0) * t0
	SumEnergy += complex(1.0, 0.0)

	sig_dd := complex(-1.0, 0.0) * t0 * cmplx.Exp(1.0i*cmplx.Acos(SumEnergy))

	s[0][0] = cmplx.Conj(BT_Mat[0][0])*sig_uu*BT_Mat[0][0] + cmplx.Conj(BT_Mat[1][0])*sig_dd*BT_Mat[1][0]
	s[0][1] = cmplx.Conj(BT_Mat[0][0])*sig_uu*BT_Mat[0][1] + cmplx.Conj(BT_Mat[1][0])*sig_dd*BT_Mat[1][1]
	s[1][0] = cmplx.Conj(BT_Mat[0][1])*sig_uu*BT_Mat[0][0] + cmplx.Conj(BT_Mat[1][1])*sig_dd*BT_Mat[1][0]
	s[1][1] = cmplx.Conj(BT_Mat[0][1])*sig_uu*BT_Mat[0][1] + cmplx.Conj(BT_Mat[1][1])*sig_dd*BT_Mat[1][1]

	return s
}
Ejemplo n.º 13
0
// Performs a DFT or a partial DFT on in, storing the output in out
// if start == 0 && stride == 1 the DFT is done on the entire array,
// otherwise it is done on every stride elements, starting at start.
func DFT(in, out []complex128, start, stride int) {
	N := len(in) / stride
	if N == 1 {
		out[start] = in[start]
		return
	}
	factor := -2 * math.Pi * complex(0, 1) / complex(float64(N), 0)
	for k := 0; k < N; k++ {
		out[start+k*stride] = 0
		for n := start; n < len(in); n += stride {
			out[start+k*stride] += in[n] * cmplx.Exp(factor*complex(float64(k*(n/stride)), 0))
		}
	}
}
Ejemplo n.º 14
0
func genTwiddle(stage0, stage1, s int) *Cmplx32v {
	var factor float64 = 32768.
	t := make([]complex128, stage0*stage1*s)
	for s0 := 0; s0 < stage0; s0 += 1 {
		for s1 := 0; s1 < stage1; s1 += 1 {
			for s2 := 0; s2 < s; s2 += 1 {
				p := -1. * math.Pi * 2. * float64(s0) * float64(s1) / float64(stage0*stage1)
				a := complex(factor, 0.) * cmplx.Exp(complex(0., p))
				t[s0*stage1*s+s1*s+s2] = complex(imag(a), real(a))
			}
		}
	}
	return ToM128Buf(t)
}
Ejemplo n.º 15
0
Archivo: main.go Proyecto: xunatai/ft
func fft(input, output []complex64, dir float64, stride int) {
	N := len(output)
	if N == 1 {
		output[0] = input[0]
		return
	}
	N2 := N / 2
	fft(input, output[:N2], dir, stride*2)
	fft(input[stride:], output[N2:], dir, stride*2)
	for fx := 0; fx < N2; fx++ {
		o := output[fx+N2]
		o *= complex64(cmplx.Exp(complex(0.0, dir*float64(fx)/float64(N))))
		output[fx+N2] = output[fx] - o
		output[fx] += o
	}
}
Ejemplo n.º 16
0
Archivo: plot.go Proyecto: penten/sound
func dftWindow(xj []float64) []float64 {
	// X_k = \sum_{n=0}^{N-1}x_n e^{-i 2 \pi kn/N}
	// Up to N/2 since second half is mirrored
	N := len(xj)
	Xj := make([]float64, N/2)

	// start at one to ignore the first "flat line" frequency
	for k := 1; k < N/2; k++ {
		Xk := complex(0, 0)
		for n := 0; n < N; n++ {
			exp := float64(-2.0 * math.Pi * float64(k) * float64(n) / float64(N))
			Xk += complex(xj[n], 0) * cmplx.Exp(complex(0, exp))
		}
		Xj[k] = cmplx.Abs(Xk)
	}

	return Xj
}
Ejemplo n.º 17
0
func init() {
	n_factors = make([]complex128, num_n_factors)
	for i := range n_factors {
		n_factors[i] = -2 * math.Pi * J * complex(1.0/float64(i), 0.0)
	}

	base_kn := make([]complex128, num_n_factors)
	kn_factors = make([][]complex128, num_kn_factors)
	for i := range kn_factors {
		kn_factors[i] = base_kn[i*num_kn_factors : (i+1)*num_kn_factors]
	}
	for i := range kn_factors {
		for j := range kn_factors {
			kfactor := n_factors[j] * complex(float64(i), 0.0)
			kn_factors[i][j] = cmplx.Exp(kfactor)
		}
	}
}
Ejemplo n.º 18
0
func IFFT_C(samples vlib.VectorC, N int) vlib.VectorC {

	samples.Resize(N)
	fbins := vlib.NewVectorC(N)
	normalize := complex(math.Sqrt(1.0/float64(N)), 0)
	result := vlib.NewVectorC(N)
	for i := 0; i < N; i++ {
		for n := 0; n < N; n++ {
			scale := float64(i) * float64(n) / float64(N)
			binf := complex(0, 2.0*math.Pi*scale)
			fbins[n] = cmplx.Exp(binf)
		}
		// fbins = fbins.ScaleC(i)
		// fmt.Print("\ni=", i, fbins)
		result[i] = vlib.DotC(samples, fbins) * normalize
	}

	return result
}
Ejemplo n.º 19
0
Archivo: main.go Proyecto: xunatai/ft
func (ft *FT) decodeSpectrum(filename string) {
	im := loadImage(filename)
	ft.w = im.Bounds().Dx() / ft.mode
	ft.h = im.Bounds().Dy()
	ft.initData()

	for x := 0; x < ft.w; x++ {
		for y := 0; y < ft.h; y++ {
			for i := 0; i < ft.mode; i++ {
				f := func(r, g, b float64) complex128 {
					H, _, L := RGBToHSL(r, g, b)
					L *= math.Log(float64(ft.w * ft.h))
					return cmplx.Exp(complex(L, 2.0*math.Pi*H))
				}
				ft.channels.StoreComplex(i, x, y, im.At(x+ft.w*i, y), f)
			}
		}
	}
}
Ejemplo n.º 20
0
// FreqZ return frequency response given filter coefficients.
// The name of this function is followed by Matlab.
func FreqZ(b, a []float64, N int) []complex128 {
	H := make([]complex128, N)

	for n := 0; n < N; n++ {
		z := cmplx.Exp(complex(0.0, -2.0*math.Pi*float64(n)/float64(N)))
		numerator, denominator := complex(0, 0), complex(0, 0)
		for i := range b {
			numerator += complex(b[len(b)-1-i], 0.0) *
				cmplx.Pow(z, complex(float64(i), 0))
		}
		for i := range a {
			denominator += complex(a[len(a)-1-i], 0.0) *
				cmplx.Pow(z, complex(float64(i), 0))
		}
		H[n] = numerator / denominator
	}

	return H
}
Ejemplo n.º 21
0
func GoFFTPerK(outputSymbol gocomm.Complex128Channel, inputsamples vlib.VectorC, k, N int, inverse bool) {

	kbyN := float64(k) / float64(N)
	normalize := complex(1.0/math.Sqrt(float64(N)), 0)
	if !inverse {
		kbyN = kbyN * -1.0
	}

	fbins := vlib.NewVectorC(N)

	for n := 0; n < N; n++ {
		scale := kbyN * float64(n)
		binf := complex(0, 2.0*math.Pi*scale)
		fbins[n] = cmplx.Exp(binf)
	}
	result := vlib.GoDotC(inputsamples, fbins, 4) * normalize
	// result := vlib.DotC(inputsamples, fbins) * normalize

	var data gocomm.SComplex128Obj
	data.Ch = result
	outputSymbol <- data
}
Ejemplo n.º 22
0
//QR Decomposition using  Householder reflections
func (this *Matrix) QR() (Q1, R1 *Matrix) {
	n := this.n //rows
	m := this.m //columns

	last := n - 1

	var alpha complex128
	Q := I(m)
	if m == n {
		last--
	}
	Ai := this.Copy()

	for i := 0; i <= last; i++ {
		b := Ai.GetSubMatrix(i+1, i+1, m-i+1, n-i+1)
		x := b.GetColumn(1)

		e := NullMatrix(x.m, 1)
		e.SetValue(i+1, 1, 1)

		x1 := x.GetValue(i+1, 1)

		alpha = cmplx.Exp(complex(0, -math.Atan2(imag(x1), real(x1)))) * complex(x.FrobeniusNorm(), 0)

		u, _ := Sustract(x, e.Scalar(alpha))
		v := u.UnitVector()

		hht, _ := v.HouseholderTrasformation()

		h := SetSubMatrixToI(m, i+1, hht)

		Q = Product(Q, h)

		Ai = Product(h, Ai)

	}
	return Q, Ai
}
Ejemplo n.º 23
0
func computeTable(n int, centre_hz, Δt float64) (r []complex64) {
	const π = math.Pi
	ω1 := -2 * π * centre_hz
	//σ := 2 * π * 100.0

	r = make([]complex64, n)
	for i, _ := range r {
		t := float64(i) * Δt

		// Notch filter.
		r[i] = complex64(cmplx.Exp(complex(0.0, ω1*t)))

		/*
			 // Gaussian filter.
			for f := centre_hz - 1000.0; f < centre_hz + 1000.0; f += 1.0 {
				ω0 := 2 * π * f
				g := math.Exp( - (ω0 - ω1)*(ω0 - ω1) / (2*σ*σ))
				r[i] += complex(g, 0) *  cmplx.Exp(complex(0, ω0*t))
			}
		*/
	}

	return r
}
func test2(val, val0 complex128) complex128 {
	return val*val*cmplx.Exp(val) + val0
}
Ejemplo n.º 25
0
	"os"
	"runtime"
	"strconv"
	"sync"
	"time"
)

type ComplexFunc func(complex128) complex128

var Funcs []ComplexFunc = []ComplexFunc{
	func(z complex128) complex128 { return z*z - 0.61803398875 },
	func(z complex128) complex128 { return z*z + complex(0, 1) },
	func(z complex128) complex128 { return z*z + complex(-0.835, -0.2321) },
	func(z complex128) complex128 { return z*z + complex(0.45, 0.1428) },
	func(z complex128) complex128 { return z*z*z + 0.400 },
	func(z complex128) complex128 { return cmplx.Exp(z*z*z) - 0.621 },
	func(z complex128) complex128 { return (z*z+z)/cmplx.Log(z) + complex(0.268, 0.060) },
	func(z complex128) complex128 { return cmplx.Sqrt(cmplx.Sinh(z*z)) + complex(0.065, 0.122) },
}

func RunJulia() {
	t := time.Now()
	for n, fn := range Funcs {
		err := CreatePng("picture-"+strconv.Itoa(n)+".png", fn, 1024)

		if err != nil {
			log.Fatal(err)
		}
	}
	fmt.Println("Time passed: ", time.Since(t).Seconds())
}
Ejemplo n.º 26
0
func ApplyDopplerCorrections(
	signal_path string,
	sample_type pb.IQParams_Type,
	doppler_path, output_path string) error {

	read_sample := binary.GetReadSampleFunc(sample_type)
	if read_sample == nil {
		log.Printf("Invalid sample type")
		return errors.New("Invalid sample type")
	}

	signal_file, err := os.Open(signal_path)
	if err != nil {
		log.Printf("Error opening signal file: %s", err.Error())
		return err
	}
	doppler_file, err := os.Open(doppler_path)
	if err != nil {
		log.Printf("Error opening doppler file: %s", err.Error())
		return err
	}
	output_file, err := os.Create(output_path)
	if err != nil {
		log.Printf("Error opening output file: %s", err.Error())
		return err
	}

	// Buffered io gives a speedup of 6x!
	r := bufio.NewReader(signal_file)
	w := bufio.NewWriter(output_file)

	n := 0

	last_doppler, err := readDopplerPair(doppler_file)
	if err != nil {
		log.Printf("Doppler read error: %s", err.Error())
		return err
	}
	next_doppler, err := readDopplerPair(doppler_file)
	if err != nil {
		log.Printf("Doppler read error: %s", err.Error())
		return err
	}
	has_more_dopplers := true

	for {
		c, err := read_sample(r)
		if err != nil {
			break
		}

		if n > next_doppler.sample_num && has_more_dopplers {
			// We need a new doppler pair.
			d, err := readDopplerPair(doppler_file)
			if err != nil {
				// We've run out of dopplers. Do nothing.
				has_more_dopplers = false
			} else {
				last_doppler = next_doppler
				next_doppler = d
			}
		}

		// exp(-i 2πΔf t)
		frac := last_doppler.delta_frac
		corrector := cmplx.Exp(complex(0.0, -2*math.Pi*frac*float64(n)))
		c = c * complex64(corrector)

		err = binary.WriteComplex64LE(w, c)
		if err != nil {
			log.Printf(
				"Error writing output sample: %s", err.Error())
			return err
		}

		n++
	}

	log.Printf("Doppler corrected %d samples.\n", n)
	signal_file.Close()
	doppler_file.Close()
	output_file.Close()

	return nil
}
Ejemplo n.º 27
0
func twiddle(f, N int) complex128 {
	d := -2 * math.Pi * J * complex(float64(f), 0) / complex(float64(N), 0)
	m := cmplx.Exp(d)
	return m
}
Ejemplo n.º 28
0
func LogisticRegression(alpha complex128, Tolerance complex128, ts *TrainingSet) *Hypothesis {
	f := func(x complex128) complex128 { return 1 / (1 + cmplx.Exp(-1.0*x)) }
	hy := GradientDescent(alpha, Tolerance, ts, f)
	return hy
}
Ejemplo n.º 29
0
// ExampleExp computes Euler's identity.
func ExampleExp() {
	fmt.Printf("%.1f", cmplx.Exp(1i*math.Pi)+1)
	// Output: (0.0+0.0i)
}
Ejemplo n.º 30
0
func Essential(z complex128) complex128 {
	return cmplx.Exp(1.0 / z)
}