2

I am trying to use this algorithm to give me a Fast Fourier Transform. My compiler is choking on the sqrt() function inside vDSP_vsmul() function when i copy it over. line 41 of enter link description here

The error says cannot find overload for sqrt that accepts argument list of type '([(Float)])'. Anyone know what this part of the function is trying to do? The code appears to be trying to take the square root of an array of floats which seems very odd and I can only assume it once was able to compile prior to ios 8.4 as that Surge library is pretty heavily starred. The function looks like:

import Accelerate

// MARK: Fast Fourier Transform

public func fft(input: [Float]) -> [Float] {
var real = [Float](input)
var imaginary = [Float](count: input.count, repeatedValue: 0.0)
var splitComplex = DSPSplitComplex(realp: &real, imagp: &imaginary)

let length = vDSP_Length(floor(log2(Float(input.count))))
let radix = FFTRadix(kFFTRadix2)
let weights = vDSP_create_fftsetup(length, radix)
vDSP_fft_zip(weights, &splitComplex, 1, length, FFTDirection(FFT_FORWARD))

var magnitudes = [Float](count: input.count, repeatedValue: 0.0)
vDSP_zvmags(&splitComplex, 1, &magnitudes, 1, vDSP_Length(input.count))

var normalizedMagnitudes = [Float](count: input.count, repeatedValue: 0.0)
vDSP_vsmul(sqrt(magnitudes), 1, [2.0 / Float(input.count)], &normalizedMagnitudes, 1, vDSP_Length(input.count))

vDSP_destroy_fftsetup(weights)

return normalizedMagnitudes
}
Kevin
  • 241
  • 2
  • 9

2 Answers2

4

Magnitudes is an array [Float], but sqrt accepts only single Float. If you need to run sqrt on the whole array, you need to use map or one of the Accelerate methods.

Update: It looks like the sample code in the original post comes from a custom framework, which has its own sqrt([Float]) -> [Float].

MirekE
  • 11,515
  • 5
  • 35
  • 28
  • Right, so this should instead be a summation of the values in magnitudes and take the square root of that? – Kevin Aug 10 '15 at 23:41
  • It looks like the framework you mentioned has its own sqrt method that works on arrays: https://github.com/mattt/Surge/blob/master/Source/Arithmetic.swift. You probably miss this file in your project. – MirekE Aug 10 '15 at 23:49
0

magnitudes is the array of floats. e.g [1.6, 3.6]

In swift -> sqrt(Double) accepts parameter type Double not Array. e.g sqrt(25.0)

Problem is you are passing Array to sqrt. sqrt([25.0, 36.0])

Sameer
  • 201
  • 2
  • 4