// File: /workspace/github_cpp_makefile_star2_O2/hdlguy[P]fft_demo/demo.cpp
// Function: main
int main(int argc, char **argv) {
    const int N  = 4096;
    const int P = 16;
    const double Fs = 327.68e6;
    const double Fc = (+500.1/N)*Fs;
    const double Wc = 2*M_PI*Fc/Fs;
    const int B = 10;
    const double A = pow(2.0,(B-1))-1.0;
    cout << "A = " << A << "\n";

    vector<double> real_vec(N);
    vector<double> imag_vec(N);

    // synthesize quantized sinusoids
    for(int i=0; i<N; i++) {
        real_vec[i] = round(A*cos(Wc*i));
        imag_vec[i] = round(A*sin(Wc*i));
    }

    // window the data for spectral analysis
    window(&real_vec);
    window(&imag_vec);

    // zero pad the vectors.
    vector<double> real_vec_pad(N*P, 0.0);
    vector<double> imag_vec_pad(N*P, 0.0);
    for (int i=0; i<N; i++) real_vec_pad[i] = real_vec[i];
    for (int i=0; i<N; i++) imag_vec_pad[i] = imag_vec[i];

    // plot time domain data
    FILE* gnuplotPipe = popen ("gnuplot -persistent", "w");
    fprintf(gnuplotPipe, "plot '-' ps 0.1 lt 6\n");
    for (int i=0; i<N*P; i++) fprintf(gnuplotPipe, "%lf\n", real_vec_pad[i]);
    fprintf(gnuplotPipe, "e");
    pclose(gnuplotPipe);

    // commpute and convert to power
    Fft::transform(real_vec_pad, imag_vec_pad);
    vector<double> powvec(N*P);
    for(int i=0; i<N*P; i++) powvec[i] = pow(real_vec_pad[i]/A,2.0) + pow(imag_vec_pad[i]/A,2.0);

    // Plot frequency domain data in dB
    gnuplotPipe = popen ("gnuplot -persistent", "w");
    fprintf(gnuplotPipe, "plot '-' ps 0.1 lt 6\n");
    for (int i=0; i<N*P; i++) fprintf(gnuplotPipe, "%lf\n", 10*log10(powvec[i]));
    fprintf(gnuplotPipe, "e");
    pclose(gnuplotPipe);

    return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x88, %rsp
movq 0x3082(%rip), %rdi      # 0x4fd8
leaq 0x117b(%rip), %rsi      # 0x30d8
callq 0x10d0
movsd 0x1156(%rip), %xmm0     # 0x30c0
movq %rax, %rdi
callq 0x1130
leaq 0x118e(%rip), %rsi      # 0x3107
movq %rax, %rdi
callq 0x10d0
leaq -0xa0(%rbp), %rdi
leaq -0x68(%rbp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x1cee
leaq -0x68(%rbp), %rdi
leaq -0x38(%rbp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x1cee
xorl %ebx, %ebx
cmpq $0x1000, %rbx           # imm = 0x1000
je 0x200b
cvtsi2sd %ebx, %xmm0
mulsd 0x1109(%rip), %xmm0     # 0x30c8
movsd %xmm0, -0x70(%rbp)
callq 0x1170
movsd 0x10ef(%rip), %xmm1     # 0x30c0
mulsd %xmm1, %xmm0
callq 0x1060
movq -0xa0(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
movsd -0x70(%rbp), %xmm0
callq 0x1030
mulsd 0x10c8(%rip), %xmm0     # 0x30c0
callq 0x1060
movq -0x68(%rbp), %rax
movsd %xmm0, (%rax,%rbx,8)
incq %rbx
jmp 0x1faa
leaq -0xa0(%rbp), %rdi
callq 0x1ea8
leaq -0x68(%rbp), %rdi
callq 0x1ea8
leaq -0x50(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x38(%rbp), %rdi
leaq -0x88(%rbp), %rcx
movl $0x10000, %esi          # imm = 0x10000
callq 0x227e
leaq -0x88(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x50(%rbp), %rdi
leaq -0x19(%rbp), %rcx
movl $0x10000, %esi          # imm = 0x10000
callq 0x227e
movq -0xa0(%rbp), %rax
movq -0x38(%rbp), %rcx
xorl %edx, %edx
cmpq $0x1000, %rdx           # imm = 0x1000
je 0x207f
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
jmp 0x2067
movq -0x68(%rbp), %rax
movq -0x50(%rbp), %rcx
xorl %edx, %edx
cmpq $0x1000, %rdx           # imm = 0x1000
je 0x20a1
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
jmp 0x2089
leaq 0x1035(%rip), %rdi      # 0x30dd
leaq 0x1042(%rip), %rsi      # 0x30f1
callq 0x1040
movq %rax, %rbx
leaq 0x1035(%rip), %rdi      # 0x30f3
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x1180
xorl %r15d, %r15d
leaq 0x1033(%rip), %r14      # 0x3109
cmpq $0x10000, %r15          # imm = 0x10000
je 0x20fb
movq -0x38(%rbp), %rax
movsd (%rax,%r15,8), %xmm0
movq %rbx, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x1110
incq %r15
jmp 0x20d6
pushq $0x65
popq %rdi
movq %rbx, %rsi
callq 0x10f0
movq %rbx, %rdi
callq 0x1100
leaq -0x38(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x12cc
leaq -0x88(%rbp), %rdi
leaq -0x19(%rbp), %rdx
movl $0x10000, %esi          # imm = 0x10000
callq 0x1cee
movq -0x38(%rbp), %rax
movq -0x50(%rbp), %rcx
movq -0x88(%rbp), %rdx
xorl %esi, %esi
movsd 0xf77(%rip), %xmm2      # 0x30c0
cmpq $0x10000, %rsi          # imm = 0x10000
je 0x217a
movsd (%rax,%rsi,8), %xmm0
divsd %xmm2, %xmm0
mulsd %xmm0, %xmm0
movsd (%rcx,%rsi,8), %xmm1
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rsi,8)
incq %rsi
jmp 0x2149
leaq 0xf5c(%rip), %rdi       # 0x30dd
leaq 0xf69(%rip), %rsi       # 0x30f1
callq 0x1040
movq %rax, %rbx
leaq 0xf5c(%rip), %rdi       # 0x30f3
pushq $0x15
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x1180
xorl %r15d, %r15d
leaq 0xf5a(%rip), %r14       # 0x3109
cmpq $0x10000, %r15          # imm = 0x10000
je 0x21e4
movq -0x88(%rbp), %rax
movsd (%rax,%r15,8), %xmm0
callq 0x1080
mulsd 0xefe(%rip), %xmm0      # 0x30d0
movq %rbx, %rdi
movq %r14, %rsi
movb $0x1, %al
callq 0x1110
incq %r15
jmp 0x21af
pushq $0x65
popq %rdi
movq %rbx, %rsi
callq 0x10f0
movq %rbx, %rdi
callq 0x1100
leaq -0x88(%rbp), %rdi
callq 0x1dd2
leaq -0x50(%rbp), %rdi
callq 0x1dd2
leaq -0x38(%rbp), %rdi
callq 0x1dd2
leaq -0x68(%rbp), %rdi
callq 0x1dd2
leaq -0xa0(%rbp), %rdi
callq 0x1dd2
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x223c
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x1dd2
jmp 0x224d
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x1dd2
jmp 0x225b
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x1dd2
jmp 0x2269
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x1dd2
movq %rbx, %rdi
callq 0x1160
nop