// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq 0x2f4e(%rip), %rbx      # 0x4fd8
leaq 0x1047(%rip), %rsi      # 0x30d8
movl $0x4, %edx
movq %rbx, %rdi
callq 0x1100
movsd 0x101a(%rip), %xmm0     # 0x30c0
movq %rbx, %rdi
callq 0x1150
leaq 0x1052(%rip), %rsi      # 0x3107
movl $0x1, %edx
movq %rax, %rdi
callq 0x1100
movl $0x8000, %edi           # imm = 0x8000
callq 0x10f0
movq %rax, %rbx
movq %rax, -0x90(%rbp)
leaq 0x8000(%rax), %r14
movq %r14, -0x80(%rbp)
movl $0x8000, %edx           # imm = 0x8000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r14, -0x88(%rbp)
movl $0x8000, %edi           # imm = 0x8000
callq 0x10f0
movq %rax, %r12
movq %rax, -0x78(%rbp)
movq %rax, %r14
addq $0x8000, %r14           # imm = 0x8000
movq %r14, -0x68(%rbp)
xorl %r15d, %r15d
movl $0x8000, %edx           # imm = 0x8000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r14, -0x70(%rbp)
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0xf8c(%rip), %xmm0      # 0x30c8
movsd %xmm0, -0x30(%rbp)
callq 0x1190
movsd 0xf72(%rip), %xmm1      # 0x30c0
mulsd %xmm1, %xmm0
callq 0x1060
movsd %xmm0, (%rbx,%r15,8)
movsd -0x30(%rbp), %xmm0
callq 0x1030
mulsd 0xf51(%rip), %xmm0      # 0x30c0
callq 0x1060
movsd %xmm0, (%r12,%r15,8)
incq %r15
cmpq $0x1000, %r15           # imm = 0x1000
jne 0x212c
movq %r12, -0x30(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x1fcc
leaq -0x78(%rbp), %rdi
callq 0x1fcc
movl $0x80000, %edi          # imm = 0x80000
callq 0x10f0
movq %rax, %r15
movq %rax, -0x60(%rbp)
movq %rax, %r14
addq $0x80000, %r14          # imm = 0x80000
movq %r14, -0x50(%rbp)
movl $0x80000, %edx          # imm = 0x80000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r14, -0x58(%rbp)
movl $0x80000, %edi          # imm = 0x80000
callq 0x10f0
movq %rax, %r12
movq %rax, -0x48(%rbp)
movq %rax, %r13
addq $0x80000, %r13          # imm = 0x80000
movq %r13, -0x38(%rbp)
xorl %r14d, %r14d
movl $0x80000, %edx          # imm = 0x80000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq %r13, -0x40(%rbp)
movsd (%rbx,%r14,8), %xmm0
movsd %xmm0, (%r15,%r14,8)
incq %r14
cmpq $0x1000, %r14           # imm = 0x1000
jne 0x2206
movl $0x8000, %edx           # imm = 0x8000
movq %r12, %rdi
movq -0x30(%rbp), %rsi
callq 0x10c0
leaq 0xea7(%rip), %rdi       # 0x30dd
leaq 0xeb4(%rip), %rsi       # 0x30f1
callq 0x1040
movq %rax, %r12
leaq 0xea7(%rip), %rdi       # 0x30f3
movl $0x15, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x11a0
xorl %r14d, %r14d
leaq 0xea1(%rip), %r13       # 0x3109
movsd (%r15,%r14,8), %xmm0
movq %r12, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x1130
incq %r14
cmpq $0x10000, %r14          # imm = 0x10000
jne 0x2268
movl $0x65, %edi
movq %r12, %rsi
callq 0x1110
movq %r12, %rdi
callq 0x1120
leaq -0x60(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x12fc
movl $0x80000, %edi          # imm = 0x80000
callq 0x10f0
movq %rax, %r15
xorl %r12d, %r12d
movl $0x80000, %edx          # imm = 0x80000
movq %rax, %rdi
xorl %esi, %esi
callq 0x10a0
movq -0x60(%rbp), %rax
movq -0x48(%rbp), %rcx
movsd 0xde8(%rip), %xmm2      # 0x30c0
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 0x22d8
leaq 0xdcc(%rip), %rdi       # 0x30dd
leaq 0xdd9(%rip), %rsi       # 0x30f1
callq 0x1040
movq %rax, %r12
leaq 0xdcc(%rip), %rdi       # 0x30f3
movl $0x15, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x11a0
xorl %r14d, %r14d
leaq 0xdc6(%rip), %r13       # 0x3109
movsd (%r15,%r14,8), %xmm0
callq 0x1080
mulsd 0xd7a(%rip), %xmm0      # 0x30d0
movq %r12, %rdi
movq %r13, %rsi
movb $0x1, %al
callq 0x1130
incq %r14
cmpq $0x10000, %r14          # imm = 0x10000
jne 0x2343
movl $0x65, %edi
movq %r12, %rsi
callq 0x1110
movq %r12, %rdi
callq 0x1120
movq %r15, %rdi
callq 0x10e0
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x239a
callq 0x10e0
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
movq -0x30(%rbp), %r14
je 0x23ac
callq 0x10e0
movq %r14, %rdi
callq 0x10e0
movq %rbx, %rdi
callq 0x10e0
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x23cf
movq %rax, %r15
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0x23e5
callq 0x10e0
jmp 0x23e5
movq %rax, %r15
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x23f8
callq 0x10e0
jmp 0x23f8
movq %rax, %r15
movq -0x30(%rbp), %rdi
callq 0x10e0
jmp 0x2406
movq %rax, %r15
movq %rbx, %rdi
callq 0x10e0
movq %r15, %rdi
callq 0x1180