// File: /workspace/github_cpp_makefile_star2_O2/jkingan[P]ladyheather/heather.cpp
// Function: deglitch_plot_queue(int, double)
void deglitch_plot_queue(int pid, DATA_SIZE sigma)
{
long i;
long count;
long dg_count;
long pcount;
int id, start, end;
int col;
struct PLOT_Q q0, q;
DATA_SIZE sdev, avg;
DATA_SIZE val;

   // replace all queue points that are more than 3 standard deviations away
   // from the average value.  If pid < 0 all plots are deglitched, otherwise
   // only the specified plot is deglitched.


   if(queue_interval == 0) return;
   if(stat_count == 0) return;
   if(pid > (NUM_PLOTS+DERIVED_PLOTS)) return;

   if(pid < 0) {  // do all plots
      start = 0;
      end = (NUM_PLOTS+DERIVED_PLOTS-1);
if(debug_file) {
   fprintf(debug_file, "deglitch all plots: sigma:%g\n", sigma);
}
   }
   else {  // do single specified plot
      start = end = pid;
if(debug_file) {
   fprintf(debug_file, "deglitch plot %s: sigma:%g\n", plot[pid].plot_id,sigma);
}
   }

   count = 0;
   dg_count = 0;
   col = 0;
   i = plot_q_col0;   // deglitch displayed points
   pcount = plot_q_size;
////i = 0;            // deglitch all queue points
   while(pcount) {  // scan the data that is in the plot queue
      --pcount;
      if(i > 0) q0 = get_plot_q(i-1); // previous point
      else      q0 = get_plot_q(i);
      q = get_plot_q(i);              // point to deglitch

      for(id=start; id<=end; id++) {  // deglitch each plots' entries
         if(rcvr_type == CS_RCVR) ;        // don't deglitch message offset/jitter data
         else if(rcvr_type == TICC_RCVR) ;
         else if(id == NINE) continue;
         else if(id == TEN) continue;

         sdev = ((plot[id].sum_yy/stat_count) - ((plot[id].sum_y/stat_count)*(plot[id].sum_y/stat_count)) );
         if(sdev < (DATA_SIZE) 0.0) sdev = (DATA_SIZE) 0.0 - sdev;
         sdev = (DATA_SIZE) sqrt(sdev);

         avg = (DATA_SIZE) (plot[id].sum_y/stat_count);

         val = (DATA_SIZE) q.data[id]/(DATA_SIZE) queue_interval;

         if(val < (avg - (sdev*sigma))) {
            q.data[id] = q0.data[id];
            ++dg_count;
//sprintf(debug_text, "deglitch < %s:  #%ld  queue %ld   sdev:%g  val:%g  avg:%g", plot[id].plot_id, dg_count, i, sdev, val, avg);
if(debug_file) fprintf(debug_file, "deglitch < %s:  #%ld   queue %ld   sdev:%g  val:%g  avg:%g\n", plot[id].plot_id, dg_count, i, sdev, val, avg);
            put_plot_q(i, q);

         }
         else if(val > (avg + (sdev*sigma))) {
            q.data[id] = q0.data[id];
            ++dg_count;
//sprintf(debug_text, "deglitch > %s:  #%ld  queue %ld   sdev:%g  val:%g  avg:%g", plot[id].plot_id, dg_count, i, sdev, val, avg);
if(debug_file) fprintf(debug_file, "deglitch > %s:  #%ld   queue %ld   sdev:%g  val:%g  avg:%g\n", plot[id].plot_id, dg_count, i, sdev, val, avg);
            put_plot_q(i, q);
         }
      }

      ++i;
      if(i >= plot_q_size) i = 0;
      if(++count > plot_q_count) break;  // deglitch all points

      col += plot_mag;    // deglitch displayed points
      if(col >= (PLOT_WIDTH*view_interval)) break;
   }
// sprintf(debug_text3, "count:%ld  col:%d", count,col);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movsd %xmm0, -0x30(%rbp)
cmpl $0x14, %edi
jg 0xff88
cmpq $0x0, 0x1f56e2(%rip)    # 0x205398
je 0xff88
movsd 0x2e72cc(%rip), %xmm0   # 0x2f6f90
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xfcd4
jnp 0xff88
movl %edi, %r14d
movq 0x2186b2(%rip), %rdi    # 0x228390
testl %r14d, %r14d
js 0xfd15
testq %rdi, %rdi
je 0xfd10
movl %r14d, %eax
imulq $0xe8, %rax, %rax
leaq 0x138037(%rip), %rcx    # 0x147d30
movq (%rax,%rcx), %rdx
leaq 0xe4483(%rip), %rsi     # 0xf4187
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x9560
movl %r14d, %r13d
jmp 0xfd34
pushq $0x13
popq %r13
xorl %r14d, %r14d
testq %rdi, %rdi
je 0xfd34
leaq 0xe4441(%rip), %rsi     # 0xf4169
movsd -0x30(%rbp), %xmm0
movb $0x1, %al
callq 0x9560
movq 0x1f55e5(%rip), %rcx    # 0x205320
testq %rcx, %rcx
je 0xff88
movq 0x1f551d(%rip), %rbx    # 0x205268
movl %r14d, %eax
movq %rax, -0x48(%rbp)
imulq $0xe8, %rax, %rax
leaq 0x137fd0(%rip), %rdx    # 0x147d30
addq %rax, %rdx
movq %rdx, -0x40(%rbp)
xorl %r14d, %r14d
movq $0x0, -0x38(%rbp)
xorl %esi, %esi
movq %rsi, -0x50(%rbp)
decq %rcx
movq %rcx, -0x58(%rbp)
xorl %eax, %eax
testq %rbx, %rbx
setg %al
movq %rbx, %rsi
subq %rax, %rsi
leaq -0x1c0(%rbp), %rdi
callq 0xf9c7
leaq -0x110(%rbp), %rdi
movq %rbx, %rsi
callq 0xf9c7
imulq $0xb0, %rbx, %rax
movq %rax, -0x60(%rbp)
movq -0x40(%rbp), %r12
movq -0x48(%rbp), %r15
cmpl %r15d, %r13d
jl 0xff2f
movq 0x1f2b05(%rip), %rax    # 0x2028d0
cmpq $0x10000, %rax          # imm = 0x10000
je 0xfdec
cmpq $0x20000, %rax          # imm = 0x20000
je 0xfdec
movl %r15d, %eax
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
cmpl $0xc, %eax
je 0xff20
movsd 0x68(%r12), %xmm0
movsd 0x78(%r12), %xmm1
movsd 0x2e718e(%rip), %xmm2   # 0x2f6f90
divsd %xmm2, %xmm1
divsd %xmm2, %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
subsd %xmm1, %xmm2
movapd %xmm1, %xmm0
xorpd %xmm3, %xmm3
cmpltsd %xmm3, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
ucomisd %xmm3, %xmm0
jb 0xfe3f
sqrtsd %xmm0, %xmm0
jmp 0xfe44
callq 0x9260
movsd 0x68(%r12), %xmm2
divsd 0x2e713d(%rip), %xmm2   # 0x2f6f90
cvtsi2sdq 0x1f553c(%rip), %xmm3 # 0x205398
movsd -0x100(%rbp,%r15,8), %xmm1
divsd %xmm3, %xmm1
movapd %xmm0, %xmm3
mulsd -0x30(%rbp), %xmm3
movapd %xmm2, %xmm4
subsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jbe 0xfeb1
movsd -0x1b0(%rbp,%r15,8), %xmm3
movsd %xmm3, -0x100(%rbp,%r15,8)
incq %r14
movq 0x2184f1(%rip), %rdi    # 0x228390
testq %rdi, %rdi
je 0xfef6
movq (%r12), %rdx
leaq 0xe42f4(%rip), %rsi     # 0xf41a3
jmp 0xfee9
addsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
jbe 0xff20
movsd -0x1b0(%rbp,%r15,8), %xmm3
movsd %xmm3, -0x100(%rbp,%r15,8)
incq %r14
movq 0x2184b7(%rip), %rdi    # 0x228390
testq %rdi, %rdi
je 0xfef6
movq (%r12), %rdx
leaq 0xe42f6(%rip), %rsi     # 0xf41df
movq %r14, %rcx
movq %rbx, %r8
movb $0x3, %al
callq 0x9560
testq %rbx, %rbx
js 0xff20
cmpq %rbx, 0x1f541e(%rip)    # 0x205320
jle 0xff20
movq 0x1f5405(%rip), %rdi    # 0x205310
addq -0x60(%rbp), %rdi
movl $0xb0, %edx
leaq -0x110(%rbp), %rsi
callq 0x92a0
incq %r15
addq $0xe8, %r12
jmp 0xfdbb
movq -0x50(%rbp), %rsi
cmpq 0x1f53fe(%rip), %rsi    # 0x205338
movq -0x58(%rbp), %rcx
jge 0xff88
incq %rbx
cmpq 0x1f53d6(%rip), %rbx    # 0x205320
movl $0x0, %eax
cmovgeq %rax, %rbx
movslq 0x1f5476(%rip), %rax    # 0x2053d0
movq -0x38(%rbp), %rdx
movslq %edx, %rdx
addq %rax, %rdx
movslq 0x1f2919(%rip), %rax    # 0x202884
imulq 0x1f542d(%rip), %rax    # 0x2053a0
movq %rdx, -0x38(%rbp)
cmpq %rdx, %rax
jle 0xff88
incq %rsi
testq %rcx, %rcx
jne 0xfd74
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq