Least_Square.c 825 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdio.h>
  2. #include "libs/logger.h"
  3. //y = ax + b
  4. typedef struct {
  5. double x;
  6. double y;
  7. }smaple_t;
  8. static smaple_t samples[5];
  9. static int sample_count;
  10. static float AA, BB;
  11. void Least_square_method(void)
  12. {
  13. int i=0;
  14. double K = 0, A = 0, B = 0, C = 0, D = 0;
  15. for(i=0;i<sample_count;i++) {
  16. A += samples[i].x * samples[i].y;
  17. B += samples[i].x;
  18. C += samples[i].y;
  19. D += samples[i].x * samples[i].x;
  20. }
  21. AA = (sample_count * A - B * C) / ( sample_count * D - B * B);
  22. BB = C / sample_count - K * B / sample_count;
  23. sys_debug("Gain = %f, zero Off = %f\n", AA, BB);
  24. }
  25. void add_smaple(float x, float y){
  26. if (sample_count < 5){
  27. samples[sample_count].x = x;
  28. samples[sample_count].y = y;
  29. sample_count ++;
  30. sys_debug("add x = %f, y = %f\n", x, y);
  31. }
  32. if (sample_count == 5){
  33. Least_square_method();
  34. }
  35. }