Music to spectrogram on iOS with the accelerate framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 

68 lines
1.3 KiB

//
// FFTTest.m
// SpectogramPrototype
//
// Created by Joshua Moerman on 24/12/13.
// Copyright (c) 2013 Joshua Moerman. All rights reserved.
//
#import "FFTTest.h"
@import Accelerate;
@interface FFTTest () {
FFTSetup setup;
float * window;
float * buffer;
}
@end
const int logN = 13;
const int N = 1 << logN;
const float scaling = 1.0 / N;
@implementation FFTTest
@synthesize acceptedSize;
- (id)init{
if(self = [super init]){
acceptedSize = N;
// allocate structs
setup = vDSP_create_fftsetup(logN, kFFTRadix2);
window = calloc(N, sizeof(float));
buffer = calloc(N, sizeof(float));
assert(setup && window && buffer);
// initialize bits
vDSP_blkman_window(window, N, 0);
}
return self;
}
- (void)dealloc{
free(buffer);
free(window);
vDSP_destroy_fftsetup(setup);
}
- (void)inPlaceFFT:(float *)data forSize:(unsigned int)n{
assert(n == N);
// Windowing
vDSP_vmul(data, 1, window, 1, data, 1, N);
// Rearrange data
DSPSplitComplex split_buffer = { buffer, buffer + N/2 };
vDSP_ctoz((DSPComplex *)data, 2, &split_buffer, 1, N/2);
// DO IT!
vDSP_fft_zrip(setup, &split_buffer, 1, logN, FFT_FORWARD);
// Calculates magnitudes (so ignores phases)
vDSP_zvabs(&split_buffer, 1, data, 1, N/2);
vDSP_vsmul(data, 1, &scaling, data, 1, N/2);
}
@end