/*******************************************************************************
* Copyright 2015-2018 Intel Corporation.
*
* This software and the related documents are Intel copyrighted  materials,  and
* your use of  them is  governed by the  express license  under which  they were
* provided to you (License).  Unless the License provides otherwise, you may not
* use, modify, copy, publish, distribute,  disclose or transmit this software or
* the related documents without Intel's prior written permission.
*
* This software and the related documents  are provided as  is,  with no express
* or implied  warranties,  other  than those  that are  expressly stated  in the
* License.
*******************************************************************************/

//   A simple example of performing the forward FFT for processing real data
//     for CCS packed formats and computing magnitude
// implemented with Intel(R) Integrated Primitives (Intel(R) IPP) functions:
//     ippiFFTGetSize_R_32f
//     ippiFFTInit_R_32f
//     ippiFFTFwd_RToPack_32f_C1R
//     ippsMagnitude_32fc


#include <stdio.h>
#include <math.h>
#include "ipp.h"

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN exitLine:                                  /* Label for Exit */
#define check_sts(st) if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if Intel(R) IPP function returned status different from ippStsNoErr */

/* Results of ippMalloc() are not validated because Intel(R) IPP functions perform bad arguments check and will return an appropriate status  */

int main(void)
{
    int i;
    IppStatus status = ippStsNoErr;
    Ipp32f pSrc[8], pDst[10];         /* Pointers to source/destination images */
    Ipp32f pMagn[4];                  /* Pointer to the computed magnitude values */
    IppsFFTSpec_R_32f* pSpec = NULL;  /* Pointer to FFT pSpec structure */

    Ipp8u *pMemInit = NULL, *pBuffer = NULL, *pSpecMem = NULL; /* Pointer to the work buffers */
    int sizeSpec = 0, sizeInit = 0, sizeBuf = 0;               /* size of FFT pSpec structure, Init and work buffers */

    check_sts( status = ippsFFTGetSize_R_32f(3, IPP_FFT_DIV_INV_BY_N, ippAlgHintNone, &sizeSpec, &sizeInit, &sizeBuf) )

    /* memory allocation */
    pSpecMem = (Ipp8u*) ippMalloc( sizeSpec );
    pBuffer  = (Ipp8u*) ippMalloc( sizeBuf );
    pMemInit = (Ipp8u*) ippMalloc( sizeInit );

    check_sts( status = ippsFFTInit_R_32f(&pSpec, 3, IPP_FFT_DIV_INV_BY_N, ippAlgHintNone, pSpecMem, pMemInit) )

    for (i = 0; i< 8 ; ++i)
    {
        pSrc[i] = (float)cos(IPP_2PI * i * 16.0/64);
    }

    check_sts( status = ippsFFTFwd_RToCCS_32f( pSrc, pDst, pSpec, NULL ) )

    check_sts( status = ippsMagnitude_32fc( (Ipp32fc*)pDst, pMagn, 4 ) )

    printf("FFT Magnitude =  %f  %f  %f  %f\n", pMagn[0], pMagn[1], pMagn[2], pMagn[3]);

EXIT_MAIN
    ippFree( pMemInit );
    ippFree( pSpec );
    ippFree( pBuffer );
    printf("Exit status %d (%s)\n", (int)status, ippGetStatusString(status));
    return (int)status;
}