-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fft * implemented rfft backward forward (#39) * rfft_forward * passed real_forward * irfft * modified to_complex * implemented irfft but memory leak occurred... (#39) * passed rfft and irfft test (#39) * fixed bug (#42) * modified vDSP rFFT * add fft --------- Co-authored-by: jjjkkkjjj-mizuno <[email protected]>
- Loading branch information
1 parent
351a86c
commit 0706806
Showing
19 changed files
with
3,204 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Accelerate | ||
|
||
|
||
var sreal: [Float] = [Float(0), 1, 0, 0] | ||
var simag: [Float] = Array(repeating: Float.zero, count: sreal.count) | ||
|
||
var real = Array(repeating: Float.zero, count: 4) | ||
var imag = Array(repeating: Float.zero, count: 4) | ||
let log2N = 2 | ||
|
||
let setup = vDSP_create_fftsetup(vDSP_Length(log2N), FFTRadix(kFFTRadix2))! | ||
let direction = kFFTDirection_Forward | ||
|
||
sreal.withUnsafeMutableBufferPointer{ | ||
srealptr in | ||
simag.withUnsafeMutableBufferPointer{ | ||
simagptr in | ||
real.withUnsafeMutableBufferPointer{ | ||
realptr in | ||
imag.withUnsafeMutableBufferPointer{ | ||
imagptr in | ||
var src = DSPSplitComplex(realp: srealptr.baseAddress!, imagp: simagptr.baseAddress!) | ||
var dst = DSPSplitComplex(realp: realptr.baseAddress!, imagp: imagptr.baseAddress!) | ||
//vDSP_fft_zrop(setup, &src, vDSP_Stride(1), &dst, vDSP_Stride(1), vDSP_Length(log2N), FFTDirection(direction)) | ||
vDSP_fft_zrip(setup, &src, vDSP_Stride(1), vDSP_Length(log2N), FFTDirection(direction)) | ||
|
||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
vDSP_destroy_fftsetup(setup) | ||
|
||
print(sreal) | ||
print(simag) | ||
|
||
sreal = [Float(0), 1, 0, 0] | ||
simag = Array(repeating: Float.zero, count: sreal.count) | ||
|
||
real = Array(repeating: Float.zero, count: 4) | ||
imag = Array(repeating: Float.zero, count: 4) | ||
|
||
let setup_dft = vDSP_DFT_zop_CreateSetup(nil, vDSP_Length(4), vDSP_DFT_Direction.FORWARD) | ||
|
||
sreal.withUnsafeMutableBufferPointer{ | ||
srealptr in | ||
simag.withUnsafeMutableBufferPointer{ | ||
simagptr in | ||
real.withUnsafeMutableBufferPointer{ | ||
realptr in | ||
imag.withUnsafeMutableBufferPointer{ | ||
imagptr in | ||
var src = DSPSplitComplex(realp: srealptr.baseAddress!, imagp: simagptr.baseAddress!) | ||
var dst = DSPSplitComplex(realp: realptr.baseAddress!, imagp: imagptr.baseAddress!) | ||
//vDSP_fft_zrop(setup, &src, vDSP_Stride(1), &dst, vDSP_Stride(1), vDSP_Length(log2N), FFTDirection(direction)) | ||
vDSP_DFT_Execute(setup, <#T##__Ir: UnsafePointer<Float>##UnsafePointer<Float>#>, <#T##__Ii: UnsafePointer<Float>##UnsafePointer<Float>#>, <#T##__Or: UnsafeMutablePointer<Float>##UnsafeMutablePointer<Float>#>, <#T##__Oi: UnsafeMutablePointer<Float>##UnsafeMutablePointer<Float>#>)(setup, &src, vDSP_Stride(1), vDSP_Length(log2N), FFTDirection(direction)) | ||
|
||
} | ||
} | ||
} | ||
|
||
} | ||
vDSP.FFT(log2n: <#T##vDSP_Length#>, radix: <#T##vDSP.Radix#>, ofType: <#T##_.Type#>).fo | ||
vDSP_destroy_fftsetup(setup) | ||
|
||
print(sreal) | ||
print(simag) |
117 changes: 117 additions & 0 deletions
117
MatftDemo/MatftDemo/accelerate.playground/Sources/New File.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import UIKit | ||
import Matft | ||
import Accelerate | ||
/* | ||
let rgb = UIImage(named: "rena.jpeg")! | ||
|
||
func convertToGrayScale(image: UIImage) -> UIImage{ | ||
let width = Int(image.size.width) | ||
let height = Int(image.size.height) | ||
let channel = Int(image.cgImage!.bitsPerPixel/8) | ||
|
||
let imageRect: CGRect = CGRect(x:0, y:0, width: width, height: height) | ||
|
||
let colorSpace = CGColorSpaceCreateDeviceGray() | ||
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue | CGImageByteOrderInfo.orderDefault.rawValue) | ||
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) | ||
|
||
context?.draw(image.cgImage!, in: imageRect) | ||
let imageRef = context!.makeImage() | ||
let newImage = UIImage(cgImage: imageRef!) | ||
return newImage | ||
} | ||
|
||
let gray = convertToGrayScale(image: rgb) | ||
|
||
|
||
var mf_rgb = Matft.image.cgimage2mfarray(rgb.cgImage!) | ||
var mf_gray = Matft.image.cgimage2mfarray(gray.cgImage!) | ||
|
||
|
||
mf_rgb = mf_rgb[Matft.reverse].to_contiguous(mforder: .Row) | ||
mf_gray = mf_gray[Matft.reverse] | ||
|
||
Matft.image.mfarray2cgimage(mf_rgb) | ||
Matft.image.mfarray2cgimage(mf_gray) | ||
|
||
/********* | ||
Row Contiguous | ||
*/ | ||
/* | ||
// resize | ||
// ref: https://developer.apple.com/documentation/accelerate/1509208-vimagescale_argbffff | ||
let newdata = MfData(size: 150*150*4, mftype: .Float) | ||
let newstructure = MfStructure(shape: [150, 150, 4], mforder: .Row) | ||
newdata.withUnsafeMutableStartRawPointer{ | ||
dstptr in | ||
mf_rgb.withUnsafeMutableStartRawPointer{ | ||
srcptr in | ||
var src_buffer = vImage_Buffer(data: srcptr, height: vImagePixelCount(225), width: vImagePixelCount(225), rowBytes: 225*4*4) | ||
var dst_buffer = vImage_Buffer(data: dstptr, height: vImagePixelCount(150), width: vImagePixelCount(150), rowBytes: 150*4*4) | ||
|
||
vImageScale_ARGBFFFF(&src_buffer, &dst_buffer, nil, vImage_Flags(kvImageHighQualityResampling)) | ||
} | ||
|
||
} | ||
|
||
let resize = MfArray(mfdata: newdata, mfstructure: newstructure) | ||
print(resize) | ||
Matft.image.mfarray2cgimage(resize) | ||
*/ | ||
|
||
/********* | ||
Column Contiguous | ||
*/ | ||
mf_rgb = mf_rgb.to_contiguous(mforder: .Column) | ||
|
||
// resize | ||
// ref: https://developer.apple.com/documentation/accelerate/1509208-vimagescale_argbffff | ||
let newdata = MfData(size: 150*150*4, mftype: .Float) | ||
let newstructure = MfStructure(shape: [150, 150, 4], mforder: .Column) | ||
newdata.withUnsafeMutableStartRawPointer{ | ||
dstptr in | ||
mf_rgb.withUnsafeMutableStartRawPointer{ | ||
srcptr in | ||
for i in 0..<4{ | ||
var src_buffer = vImage_Buffer(data: srcptr + i*225*225*4, height: vImagePixelCount(225), width: vImagePixelCount(225), rowBytes: 225*4) | ||
var dst_buffer = vImage_Buffer(data: dstptr + i*150*150*4, height: vImagePixelCount(150), width: vImagePixelCount(150), rowBytes: 150*4) | ||
|
||
vImageScale_PlanarF(&src_buffer, &dst_buffer, nil, vImage_Flags(kvImageHighQualityResampling)) | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
var resize = MfArray(mfdata: newdata, mfstructure: newstructure) | ||
Matft.image.mfarray2cgimage(resize) | ||
|
||
resize.swapaxes(axis1: -1, axis2: 0)[3] = MfArray([0.5] as [Float]) | ||
print(resize) | ||
let alpha = resize[Matft.all, Matft.all, 3~<4] | ||
resize[0~<, 0~<, 0~<3] = (resize[Matft.all, Matft.all, 0~<3]*alpha + (1 - alpha) * MfArray([1, 1, 1], mftype: resize.mftype)) | ||
print(resize) | ||
|
||
|
||
|
||
let grayed = Matft.image.color(resize, exclude_alpha: false) | ||
Matft.image.mfarray2cgimage(grayed) | ||
|
||
|
||
/* | ||
let colorSpace = CGColorSpaceCreateDeviceRGB() | ||
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue | CGImageByteOrderInfo.order32Little.rawValue | CGBitmapInfo.floatComponents.rawValue) | ||
print(resize.strides) | ||
resize.withUnsafeMutableStartRawPointer{ | ||
srcptr -> CGImage in | ||
// NOTE: Force cast to UInt8 | ||
let provider = CGDataProvider(data: CFDataCreate(kCFAllocatorDefault, srcptr.assumingMemoryBound(to: UInt8.self), 150*150*4*4)) | ||
let cgimage = CGImage(width: 150, height: 150, bitsPerComponent: 8*4, bitsPerPixel: 8*4*150*150, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo, provider: provider!, decode: nil, shouldInterpolate: false, intent: CGColorRenderingIntent.defaultIntent)! | ||
|
||
return cgimage | ||
} | ||
|
||
*/ | ||
|
||
|
||
*/ |
4 changes: 4 additions & 0 deletions
4
MatftDemo/MatftDemo/accelerate.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,11 @@ public class Matft{ | |
*/ | ||
public class image{} | ||
|
||
/** | ||
FFT | ||
*/ | ||
public class fft{} | ||
|
||
/** | ||
Complex | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.