// File: /workspace/github_c_makefile_star2_O2/stonezwr[P]SpiKL-IP/channel.C
// Function: Channel::BSA()
void Channel::BSA(){
    int length_kernel = 24;
    int length_signal = (_analog.size()*_step_analog)/_step_spikeT + 24;
    double threshold = 0.6;
    double error1, error2, temp;
    double * kernel = new double[length_kernel];
    double * signal = new double[length_signal];
    //double sig, noi;

    for(int i = 0; i < length_kernel; i++) kernel[i] = exp(-(i-double(length_kernel)/2)*(i-double(length_kernel)/2)/25);
    temp = 0;
    for(int i = 0; i < length_kernel; i++) temp += kernel[i];
    for(int i = 0; i < length_kernel; i++) kernel[i] /= temp;

    int index = 0;
    for(int i = 0; i < _analog.size(); i++)
        for(; index < (i+1)*_step_analog/_step_spikeT; index++)
            signal[index] = _analog[i]*2e3;
    for(; index < length_signal; index++) signal[index] = 0;

    int j;
    for(int i = 0; i < length_signal-24; i++){
        error1 = 0;
        error2 = 0;
        for(j = 0; j < length_kernel; j++){
            temp = signal[i+j] - kernel[j];
            error1 += (temp<0) ? -temp : temp;
            temp = signal[i+j];
            error2 += (temp<0) ? -temp : temp;
        }
        if(error1 < (error2-threshold)){
            _spikeT.push_back(i+1);
            for(j = 0; j < length_kernel; j++) signal[i+j] -= kernel[j];
        }
    }

    delete [] kernel;
    delete [] signal;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x8(%rdi), %rcx
subq (%rdi), %rcx
sarq $0x3, %rcx
movslq 0x38(%rdi), %rax
imulq %rcx, %rax
movslq 0x3c(%rdi), %rcx
xorl %ebp, %ebp
xorl %edx, %edx
divq %rcx
movq %rax, %r12
movl %r12d, %r15d
addl $0x18, %r15d
movl $0xc0, %edi
callq 0x3030
movq %rax, %r14
movslq %r15d, %r13
leaq (,%r13,8), %rax
testl %r13d, %r13d
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x3030
movq %rax, %r15
cmpq $0x18, %rbp
je 0x3a63
leal -0xc(%rbp), %eax
cvtsi2sd %eax, %xmm0
mulsd %xmm0, %xmm0
divsd 0x105b5(%rip), %xmm0    # 0x14008
callq 0x3570
movsd %xmm0, (%r14,%rbp,8)
incq %rbp
jmp 0x3a3a
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x18, %rax
je 0x3a7a
addsd (%r14,%rax,8), %xmm0
incq %rax
jmp 0x3a69
xorl %eax, %eax
cmpq $0x18, %rax
je 0x3a97
movsd (%r14,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r14,%rax,8)
incq %rax
jmp 0x3a7c
movq (%rbx), %rcx
movq 0x8(%rbx), %rsi
subq %rcx, %rsi
sarq $0x3, %rsi
movl 0x38(%rbx), %edi
movl 0x3c(%rbx), %r8d
xorl %r9d, %r9d
movsd 0x10559(%rip), %xmm0    # 0x14010
xorl %r10d, %r10d
cmpq %rsi, %r9
je 0x3af1
movslq %r10d, %r10
leaq 0x1(%r9), %r11
movl %edi, %eax
imull %r11d, %eax
cltd
idivl %r8d
cltq
cmpq %rax, %r10
jge 0x3aec
movsd (%rcx,%r9,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%r15,%r10,8)
incq %r10
jmp 0x3ad2
movq %r11, %r9
jmp 0x3aba
movslq %r10d, %rax
cmpq %r13, %rax
jge 0x3b03
andq $0x0, (%r15,%rax,8)
incq %rax
jmp 0x3af4
addq $0x18, %rbx
movslq %r12d, %r13
xorl %ebp, %ebp
movsd 0x10504(%rip), %xmm3    # 0x14018
movapd 0x10504(%rip), %xmm4    # 0x14020
cmpq %r13, %rbp
jge 0x3bb8
leaq (%r15,%rbp,8), %r12
xorpd %xmm0, %xmm0
xorl %eax, %eax
cmpq $0x18, %rax
je 0x3b5e
movsd (%r12,%rax,8), %xmm1
movapd %xmm1, %xmm2
subsd (%r14,%rax,8), %xmm2
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
movapd %xmm1, %xmm2
xorpd %xmm4, %xmm2
maxpd %xmm1, %xmm2
addpd %xmm2, %xmm0
incq %rax
jmp 0x3b2f
movapd %xmm0, %xmm1
addsd %xmm3, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
incq %rbp
ucomisd %xmm0, %xmm1
jbe 0x3b1c
movl %ebp, 0x4(%rsp)
movq %rbx, %rdi
leaq 0x4(%rsp), %rsi
callq 0x402a
movapd 0x10494(%rip), %xmm4    # 0x14020
xorl %eax, %eax
cmpq $0x18, %rax
je 0x3bab
movsd (%r12,%rax,8), %xmm0
subsd (%r14,%rax,8), %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rax
jmp 0x3b8e
movsd 0x10465(%rip), %xmm3    # 0x14018
jmp 0x3b1c
movq %r14, %rdi
callq 0x3390
movq %r15, %rdi
callq 0x3390
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop