// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1a8, %rsp            # imm = 0x1A8
movsd %xmm0, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
cmpl $0x14, %edi
jg 0x1071b
cmpq $0x0, 0x20af6a(%rip)    # 0x21b398
je 0x1071b
movsd 0x2fcb54(%rip), %xmm0   # 0x30cf90
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1044c
jnp 0x1071b
movq 0x22df3d(%rip), %rdi    # 0x23e390
cmpl $0x0, -0x38(%rbp)
js 0x1048e
testq %rdi, %rdi
je 0x104b7
movq -0x38(%rbp), %rbx
movl %ebx, %eax
imulq $0xe8, %rax, %rax
leaq 0x14d8be(%rip), %rcx    # 0x15dd30
movq (%rax,%rcx), %rdx
leaq 0xf9caa(%rip), %rsi     # 0x10a127
movsd -0x40(%rbp), %xmm0
movb $0x1, %al
callq 0x9560
movl %ebx, -0x2c(%rbp)
jmp 0x104be
movl $0x13, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
testq %rdi, %rdi
je 0x104be
leaq 0xf9c60(%rip), %rsi     # 0x10a109
movsd -0x40(%rbp), %xmm0
movb $0x1, %al
callq 0x9560
jmp 0x104be
movq -0x38(%rbp), %rax
movl %eax, -0x2c(%rbp)
movq 0x20ae5b(%rip), %rcx    # 0x21b320
testq %rcx, %rcx
je 0x1071b
movq 0x20ad93(%rip), %r14    # 0x21b268
movq -0x38(%rbp), %rax
movl %eax, %edx
decl %eax
movl %eax, -0x44(%rbp)
movq %rdx, -0x60(%rbp)
imulq $0xe8, %rdx, %rax
leaq 0x14d83e(%rip), %rdx    # 0x15dd30
addq %rax, %rdx
movq %rdx, -0x58(%rbp)
xorl %r15d, %r15d
movq $0x0, -0x50(%rbp)
xorl %esi, %esi
movq %rsi, -0x68(%rbp)
movq %rcx, -0x70(%rbp)
xorl %eax, %eax
testq %r14, %r14
setg %al
movq %r14, %rsi
subq %rax, %rsi
leaq -0x1d0(%rbp), %rdi
callq 0x10141
leaq -0x120(%rbp), %rdi
movq %r14, %rsi
callq 0x10141
movl -0x2c(%rbp), %eax
cmpl %eax, -0x38(%rbp)
jg 0x106bf
movq -0x58(%rbp), %r13
movl -0x44(%rbp), %r12d
movq -0x60(%rbp), %rbx
movq 0x20837a(%rip), %rax    # 0x2188d0
cmpq $0x10000, %rax          # imm = 0x10000
je 0x10576
cmpq $0x20000, %rax          # imm = 0x20000
je 0x10576
movl %ebx, %eax
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
cmpl $0xc, %eax
je 0x106a8
movsd 0x68(%r13), %xmm0
movsd 0x78(%r13), %xmm1
movsd 0x2fca06(%rip), %xmm2   # 0x30cf90
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 0x105c7
sqrtsd %xmm0, %xmm0
jmp 0x105cc
callq 0x9270
movsd 0x68(%r13), %xmm2
divsd 0x2fc9b6(%rip), %xmm2   # 0x30cf90
xorps %xmm3, %xmm3
cvtsi2sdq 0x20adb2(%rip), %xmm3 # 0x21b398
movsd -0x110(%rbp,%rbx,8), %xmm1
divsd %xmm3, %xmm1
movapd %xmm0, %xmm3
mulsd -0x40(%rbp), %xmm3
movapd %xmm2, %xmm4
subsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jbe 0x10638
movsd -0x1c0(%rbp,%rbx,8), %xmm3
movsd %xmm3, -0x110(%rbp,%rbx,8)
incq %r15
movq 0x22dd6a(%rip), %rdi    # 0x23e390
testq %rdi, %rdi
je 0x1067b
movq (%r13), %rdx
leaq 0xf9b0d(%rip), %rsi     # 0x10a143
jmp 0x1066e
addsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
jbe 0x106a8
movsd -0x1c0(%rbp,%rbx,8), %xmm3
movsd %xmm3, -0x110(%rbp,%rbx,8)
incq %r15
movq 0x22dd32(%rip), %rdi    # 0x23e390
testq %rdi, %rdi
je 0x1067b
movq (%r13), %rdx
leaq 0xf9b11(%rip), %rsi     # 0x10a17f
movq %r15, %rcx
movq %r14, %r8
movb $0x3, %al
callq 0x9560
testq %r14, %r14
js 0x106a8
cmpq %r14, 0x20ac99(%rip)    # 0x21b320
jle 0x106a8
imulq $0xb0, %r14, %rdi
addq 0x20ac79(%rip), %rdi    # 0x21b310
movl $0xb0, %edx
leaq -0x120(%rbp), %rsi
callq 0x92b0
incq %rbx
incl %r12d
addq $0xe8, %r13
cmpl %r12d, -0x2c(%rbp)
jg 0x1054f
movq -0x68(%rbp), %rsi
cmpq 0x20ac6e(%rip), %rsi    # 0x21b338
movq -0x70(%rbp), %rcx
jge 0x1071b
incq %r14
cmpq 0x20ac46(%rip), %r14    # 0x21b320
movl $0x0, %eax
cmovgeq %rax, %r14
movslq 0x20ace6(%rip), %rax    # 0x21b3d0
movq -0x50(%rbp), %rdx
movslq %edx, %rdx
addq %rax, %rdx
movslq 0x208189(%rip), %rax    # 0x218884
imulq 0x20ac9d(%rip), %rax    # 0x21b3a0
movq %rdx, -0x50(%rbp)
cmpq %rdx, %rax
jle 0x1071b
decq %rcx
incq %rsi
testq %rcx, %rcx
jne 0x10506
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq