 
         frequency = newFrequency
 
-        var lengthExp: Int = Int(floor(log2(period() / discreteTime)))
-        var length: Int = Int(pow(2, Double(lengthExp)))
+        var length: Int
+        var dspLength: Int
+
+        (dspLength, length) = log2length(discreteFrequency / frequency)
+
         doubleWave = [Double](samples[0 ..< length])
-        
+
         if doubleWaveFftSetup != nil {
             destroy_fftsetup(doubleWaveFftSetup!)
             doubleWaveFftSetup = nil
         }
-        
-        
-        doubleWaveFftSetup = create_fftsetupD(vDSP_Length(lengthExp), FFTRadix(kFFTRadix2))
+
+
+        doubleWaveFftSetup = create_fftsetupD(vDSP_Length(dspLength), FFTRadix(kFFTRadix2))
     }
 
     func buidStandingWaveForFrequency(f0: Double) -> [Double] {
-        
-        /* func vDSP_fft_zrip(_ __vDSP_setup: FFTSetup,
-            _ __vDSP_ioData: UnsafePointer<DSPSplitComplex>,
-            _ __vDSP_stride: vDSP_Stride,
-            _ __vDSP_Log2N: vDSP_Length,
-            _ __vDSP_direction: FFTDirection) */
-        
-        
-        
-        var store = DSPSplitComplex(realp: <#UnsafeMutablePointer<Float>#>, imagp: <#UnsafeMutablePointer<Float>#>)
-        
-        fft_zrip(doubleWaveFftSetup, <#__vDSP_C: UnsafePointer<DSPSplitComplex>#>, <#__vDSP_IC: vDSP_Stride#>, <#__vDSP_Log2N: vDSP_Length#>, <#__vDSP_Direction: FFTDirection#>)
-
-        return approximate([Double](doubleWave[offsetOpt ..< (offsetOpt + length)]), count: figurePointCount)
+        var realPart = [Float](doubleWave)
+        var imagPart = [Float](count: doubleWave.count, repeatedValue: 0)
+
+        var signal = DSPSplitComplex(
+            realp: UnsafeMutablePointer<Float>(realPart),
+            imagp: UnsafeMutablePointer<Float>(imagPart))
+
+        fft_zrip(
+            doubleWaveFftSetup,
+            signal,
+            vDSP_Stride(1),
+            doubleWave.count,
+            FFTDirection(kFFTDirection_Forward))
+
+        var index, frac, x, y: Float
+
+        index, frac = modf(0.5 * frequency / discreteFrequency)
+
+        if index < length / 2 {
+            var index0 = Int(ceil(index))
+            var index1 = Int(floor(index))
+            x = (1 - frac) * realPart[index0] + frac * realPart[index1]
+            y = (1 - frac) * imagPart[index0] + frac * imagPart[index1]
+        } else {
+            x = realPart[index]
+            y = imagPart[index]
+        }
+
+        var phase = atan2(x, y) + M_1_PI / 2
+
+        var periodLength: Double = discreteFrequency / frequency
+        var offset: Int = Int(periodLength * phase / M_2_PI)
+
+        return approximate([Double](doubleWave[offset ..< offset + Int(length)]), count: figurePointCount)
     }
 
     func approximate(source: [Double], count: Int) -> [Double] {

    }

    func Push(sample: [Double]) {
        //NSLog(" %i ", samples.count)
        //NSLog(" %i  ", sample.count)
        
        samples += sample
        samples.removeRange(Range(start: 0, end: sample.count))
        
        doubleWave += sample
        doubleWave.removeRange(Range(start: 0, end: sample.count))
    }

    func buildSample() -> [Double] {
        return samples
    }

    func setFrequency(var newFrequency: Double) {
        
        if newFrequency < minFrequency {
            newFrequency = minFrequency
        }

            result[i] = a * t * t + b * t + c
            i = i + 1
        }

        return result
    }

    func log2length(time: Double) -> (Int, Int) {
        var lengthExp: Int = Int(floor(log2(time))) + 1
        var length: Int = Int(pow(2, Double(lengthExp)))

        return (lengthExp, length)
    }
}
