// File: /workspace/github_cpp_makefile_star2_O1/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 %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq 0x2f26(%rip), %rbx      # 0x4fd8
leaq 0x102f(%rip), %rsi      # 0x30e8
movl $0x4, %edx
movq %rbx, %rdi
callq 0x1100
movsd 0x1002(%rip), %xmm0     # 0x30d0
movq %rbx, %rdi
callq 0x1150
leaq 0x103a(%rip), %rsi      # 0x3117
movl $0x1, %edx
movq %rax, %rdi
callq 0x1100
xorl %r14d, %r14d
movq %r14, -0x90(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0xa0(%rbp)
movl $0x8000, %edi           # imm = 0x8000
callq 0x10f0
movq %rax, %rbx
movq %rax, -0xa0(%rbp)
movq %rax, -0x98(%rbp)
leaq 0x8000(%rax), %r15
movq %r15, -0x90(%rbp)
movl $0x8000, %edx           # imm = 0x8000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r15, -0x98(%rbp)
movq %r14, -0x70(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x80(%rbp)
movl $0x8000, %edi           # imm = 0x8000
callq 0x10f0
movq %rax, %r13
movq %rax, -0x80(%rbp)
movq %rax, -0x78(%rbp)
movq %rax, %r14
addq $0x8000, %r14           # imm = 0x8000
movq %r14, -0x70(%rbp)
xorl %r15d, %r15d
movl $0x8000, %edx           # imm = 0x8000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r14, -0x78(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0xf43(%rip), %xmm0      # 0x30d8
movsd %xmm0, -0x48(%rbp)
callq 0x1190
movsd 0xf29(%rip), %xmm1      # 0x30d0
mulsd %xmm1, %xmm0
callq 0x1060
movsd %xmm0, (%rbx,%r15,8)
movsd -0x48(%rbp), %xmm0
callq 0x1030
mulsd 0xf08(%rip), %xmm0      # 0x30d0
callq 0x1060
movsd %xmm0, (%r13,%r15,8)
incq %r15
cmpq $0x1000, %r15           # imm = 0x1000
jne 0x2185
leaq -0xa0(%rbp), %rdi
callq 0x1ff4
leaq -0x80(%rbp), %rdi
callq 0x1ff4
xorpd %xmm0, %xmm0
movapd %xmm0, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movl $0x80000, %edi          # imm = 0x80000
callq 0x10f0
movq %rax, -0x40(%rbp)
movq %rax, -0x38(%rbp)
movq %rax, %r14
addq $0x80000, %r14          # imm = 0x80000
movq %r14, -0x30(%rbp)
movl $0x80000, %edx          # imm = 0x80000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r14, -0x38(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x80000, %edi          # imm = 0x80000
callq 0x10f0
movq %rax, %r15
movq %rax, -0x60(%rbp)
movq %rax, -0x58(%rbp)
movq %rax, %r12
addq $0x80000, %r12          # imm = 0x80000
movq %r12, -0x50(%rbp)
xorl %r14d, %r14d
movl $0x80000, %edx          # imm = 0x80000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r12, -0x58(%rbp)
movq -0x40(%rbp), %rax
movsd (%rbx,%r14,8), %xmm0
movsd %xmm0, (%rax,%r14,8)
incq %r14
cmpq $0x1000, %r14           # imm = 0x1000
jne 0x2287
movl $0x8000, %edx           # imm = 0x8000
movq %r15, %rdi
movq %r13, %rsi
callq 0x10c0
leaq 0xe37(%rip), %rdi       # 0x30ed
leaq 0xe44(%rip), %rsi       # 0x3101
callq 0x1040
movq %rax, %r15
leaq 0xe37(%rip), %rdi       # 0x3103
movl $0x15, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x11a0
xorl %r14d, %r14d
leaq 0xe31(%rip), %r12       # 0x3119
movq -0x40(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
movq %r15, %rdi
movq %r12, %rsi
movb $0x1, %al
callq 0x1130
incq %r14
cmpq $0x10000, %r14          # imm = 0x10000
jne 0x22e8
movl $0x65, %edi
movq %r15, %rsi
callq 0x1110
movq %r15, %rdi
callq 0x1120
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x12fc
movl $0x80000, %edi          # imm = 0x80000
callq 0x10f0
movq %rax, %r15
movq %r13, -0x48(%rbp)
xorl %r12d, %r12d
movl $0x80000, %edx          # imm = 0x80000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq -0x40(%rbp), %rax
movq -0x60(%rbp), %rcx
movsd 0xd70(%rip), %xmm2      # 0x30d0
movsd (%rax,%r12,8), %xmm0
divsd %xmm2, %xmm0
mulsd %xmm0, %xmm0
movsd (%rcx,%r12,8), %xmm1
divsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%r15,%r12,8)
incq %r12
cmpq $0x10000, %r12          # imm = 0x10000
jne 0x2360
leaq 0xd54(%rip), %rdi       # 0x30ed
leaq 0xd61(%rip), %rsi       # 0x3101
callq 0x1040
movq %rax, %r12
leaq 0xd54(%rip), %rdi       # 0x3103
movl $0x15, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x11a0
xorl %r14d, %r14d
leaq 0xd4e(%rip), %r13       # 0x3119
movsd (%r15,%r14,8), %xmm0
callq 0x1080
mulsd 0xd02(%rip), %xmm0      # 0x30e0
movq %r12, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x1130
incq %r14
cmpq $0x10000, %r14          # imm = 0x10000
jne 0x23cb
movl $0x65, %edi
movq %r12, %rsi
callq 0x1110
movq %r12, %rdi
callq 0x1120
movq %r15, %rdi
callq 0x10e0
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x2422
callq 0x10e0
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
movq -0x48(%rbp), %r14
je 0x2434
callq 0x10e0
movq %r14, %rdi
callq 0x10e0
movq %rbx, %rdi
callq 0x10e0
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2457
movq %rax, %r15
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x246d
callq 0x10e0
jmp 0x246d
movq %rax, %r15
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0x2480
callq 0x10e0
jmp 0x2480
movq %rax, %r15
movq %r13, %rdi
callq 0x10e0
jmp 0x248d
movq %rax, %r15
movq %rbx, %rdi
callq 0x10e0
movq %r15, %rdi
callq 0x1180