// File: /workspace/github_c_makefile_star2_O1/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
subq $0x18, %rsp
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 %r12d, %r12d
xorl %edx, %edx
divq %rcx
movq %rax, 0x8(%rsp)
leal 0x18(%rax), %ebp
movl $0xc0, %edi
callq 0x3030
movq %rax, %r15
movslq %ebp, %r13
leaq (,%r13,8), %rax
testl %r13d, %r13d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x3030
movq %rax, %r14
leal -0xc(%r12), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd %xmm0, %xmm0
divsd 0x13068(%rip), %xmm0    # 0x17008
callq 0x35a0
movsd %xmm0, (%r15,%r12,8)
incq %r12
cmpq $0x18, %r12
jne 0x3f88
xorpd %xmm0, %xmm0
xorl %eax, %eax
addsd (%r15,%rax,8), %xmm0
incq %rax
cmpq $0x18, %rax
jne 0x3fba
xorl %eax, %eax
movsd (%r15,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r15,%rax,8)
incq %rax
cmpq $0x18, %rax
jne 0x3fcb
movq (%rbx), %rcx
movq 0x8(%rbx), %rsi
subq %rcx, %rsi
je 0x4057
sarq $0x3, %rsi
movl 0x38(%rbx), %edi
cmpq $0x1, %rsi
adcq $0x0, %rsi
movl 0x3c(%rbx), %r9d
xorl %r10d, %r10d
movsd 0x13002(%rip), %xmm0    # 0x17010
xorl %r8d, %r8d
movq %r10, %r11
incq %r10
movl %edi, %eax
imull %r10d, %eax
cltd
idivl %r9d
cmpl %eax, %r8d
jge 0x4050
movslq %r8d, %r12
cltq
leaq (%r14,%r12,8), %rdx
subq %r12, %rax
xorl %r12d, %r12d
movsd (%rcx,%r11,8), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%r12,8)
incq %r12
cmpq %r12, %rax
jne 0x4035
addl %r12d, %r8d
cmpq %rsi, %r10
jne 0x4011
jmp 0x405a
xorl %r8d, %r8d
cmpl %ebp, %r8d
jge 0x4072
movslq %r8d, %rax
movq $0x0, (%r14,%rax,8)
incq %rax
cmpq %r13, %rax
jl 0x4062
movq 0x8(%rsp), %r13
testl %r13d, %r13d
jle 0x415c
leaq 0x18(%rbx), %rax
movq %rax, 0x10(%rsp)
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %ebp, %ebp
movapd 0x12f86(%rip), %xmm5    # 0x17020
movsd 0x12f76(%rip), %xmm6    # 0x17018
movq %r14, %r12
xorpd %xmm0, %xmm0
xorl %eax, %eax
xorpd %xmm1, %xmm1
movsd (%r12,%rax,8), %xmm2
movapd %xmm2, %xmm3
movapd %xmm2, %xmm4
subsd (%r15,%rax,8), %xmm4
xorpd %xmm5, %xmm3
maxsd %xmm2, %xmm3
movapd %xmm4, %xmm2
xorpd %xmm5, %xmm2
maxsd %xmm4, %xmm2
addsd %xmm2, %xmm0
addsd %xmm3, %xmm1
incq %rax
cmpq $0x18, %rax
jne 0x40af
addsd %xmm6, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x414c
leaq 0x1(%rbp), %rax
movl %eax, 0x4(%rsp)
movq 0x20(%rbx), %rsi
cmpq 0x28(%rbx), %rsi
je 0x4110
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x20(%rbx)
jmp 0x412f
movq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x45b2
movsd 0x12ef1(%rip), %xmm6    # 0x17018
movapd 0x12ef1(%rip), %xmm5    # 0x17020
xorl %eax, %eax
movsd (%r12,%rax,8), %xmm0
subsd (%r15,%rax,8), %xmm0
movsd %xmm0, (%r12,%rax,8)
incq %rax
cmpq $0x18, %rax
jne 0x4131
incq %rbp
addq $0x8, %r12
cmpq %r13, %rbp
jb 0x40a5
movq %r15, %rdi
callq 0x33d0
movq %r14, %rdi
callq 0x33d0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop