File: autotune.c

package info (click to toggle)
gsmc 1.1-1.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 644 kB
  • ctags: 230
  • sloc: ansic: 3,000; sh: 275; makefile: 44
file content (106 lines) | stat: -rw-r--r-- 3,056 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 *   GSMC - A GTK Smith Chart Calulator for RF impedance matching
 * 
 *	(c) by Lapo Pieri IK5NAX  2003-2004,
 *          and Johannes van der Horst
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Send bugs reports, comments, critique, etc, to ik5nax@amsat.org or
 *  lapo_pieri@virgilio.it
 */


#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <stdio.h>
#include <math.h>
#include "main.h"
#include "autotune.h"
#include <unistd.h>


extern SMCDATA smcdata;
extern GtkWidget *scarea;
extern float tunestep;

#define MAXAUTOITER 50

float weigthfu(void){
  int i;
  float m, qm=0., q;
  const float p=0.6;

    m=sqrt((smcdata.rerhoIP[smcdata.ne]*smcdata.rerhoIP[smcdata.ne])+
	   (smcdata.imrhoIP[smcdata.ne]*smcdata.imrhoIP[smcdata.ne]));
 
    for(i=0; i<smcdata.ne; i++){
      q=fabs(smcdata.imrhoIP[i]/smcdata.rerhoIP[i]);
      qm+=q*q;
    }    

  return p*m+(1-p)*sqrt(qm);
}

/* Very simple "screwdriver" strategy for tuning, i.e.: adjust (aka rotate)
   each value and then pass to the next and so on restarting from the first
   if the result is not good enough. Algritm stops when error function 
   cannot go lower; error function is a weighted mean of matching and
   lowered Q.

   I don't think this could be a good algorithm, but it work in most case and
   is usefull if some manual adjust are taken; multiple invoking of autotune
   can be usefull as well as tunestep varying.

   TO BE IMPROVED!
*/
void autotune(void){
  int i, j;
  float r, oldr, wf;

  for(i=1; i<=smcdata.ne; i++){
    if(smcdata.ELlocked[i]==1)
      continue;

    r=sqrt((smcdata.rerhoIP[smcdata.ne]*smcdata.rerhoIP[smcdata.ne])+
	   (smcdata.imrhoIP[smcdata.ne]*smcdata.imrhoIP[smcdata.ne]));
    for(j=0; j<MAXAUTOITER; j++){
      oldr=r;
      smcdata.ELval0[i]*=tunestep;
      recalc();
      r=sqrt((smcdata.rerhoIP[smcdata.ne]*smcdata.rerhoIP[smcdata.ne])+
	     (smcdata.imrhoIP[smcdata.ne]*smcdata.imrhoIP[smcdata.ne]));
      if(r>oldr){
	smcdata.ELval0[i]/=tunestep;
	break;
      }
    }
    
    oldr=r;
    for(j=0; j<MAXAUTOITER; j++){
      oldr=r;
      smcdata.ELval0[i]/=tunestep;
      recalc();
      r=sqrt((smcdata.rerhoIP[smcdata.ne]*smcdata.rerhoIP[smcdata.ne])+
	     (smcdata.imrhoIP[smcdata.ne]*smcdata.imrhoIP[smcdata.ne]));
      if(r>oldr){
	smcdata.ELval0[i]*=tunestep;
	break;
      }
    }
  }
  
  drawchart(); gtk_widget_draw(scarea, NULL);
}