rusEFI
The most advanced open source ECU
knock_spectrogram.cpp
Go to the documentation of this file.
1 /**
2  * @file knock_spectrogram.cpp
3  *
4  * @date Feb 20, 2024
5  * @author Alexey Ershov, (c) 2023-2024
6  */
7 
8 #include "pch.h"
9 
10 #ifdef KNOCK_SPECTROGRAM
11 
12 #include "knock_spectrogram.h"
13 #include <climits>
14 
15 #if EFI_TEXT_LOGGING
16 static char PROTOCOL_KNOCK_SPECTROGRAMM_BUFFER[128] CCM_OPTIONAL;
17 static Logging scLogging("knock_spectrogram", PROTOCOL_KNOCK_SPECTROGRAMM_BUFFER, sizeof(PROTOCOL_KNOCK_SPECTROGRAMM_BUFFER));
18 
19 static char compressToByte(const float& v, const float& min, const float& max) {
20  float vn = (v-min) / (max-min);
21  int iv =int((float)256 * vn);
22 
23  char compressed = (char)(iv-128);
24  return compressed;
25 }
26 
27 void base64(Logging& l, const float* data, size_t size, const float& min, const float& max) {
28  static constexpr char encTable[] = {
29  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
30  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
31  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
32  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
33  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
34 
35  size_t in_len = size;
36  size_t out_len = 4 * ((in_len + 2) / 3);
37  size_t i;
38 
39  l.appendChar((char)out_len);
40 
41  for (i = 0; in_len > 2 && i < in_len - 2; i += 3) {
42  l.appendChar(encTable[(compressToByte(data[i], min, max) >> 2) & 0x3F]);
43  l.appendChar(encTable[((compressToByte(data[i], min, max) & 0x3) << 4) |
44  ((int)(compressToByte(data[i + 1], min, max) & 0xF0) >> 4)]);
45  l.appendChar(encTable[((compressToByte(data[i + 1], min, max) & 0xF) << 2) |
46  ((int)(compressToByte(data[i + 2], min, max) & 0xC0) >> 6)]);
47  l.appendChar(encTable[compressToByte(data[i + 2], min, max) & 0x3F]);
48  }
49  if (i < in_len) {
50  l.appendChar(encTable[(compressToByte(data[i], min, max) >> 2) & 0x3F]);
51 
52  if (i == (in_len - 1)) {
53  l.appendChar(encTable[((compressToByte(data[i], min, max) & 0x3) << 4)]);
54  l.appendChar('=');
55  } else {
56  l.appendChar(encTable[((compressToByte(data[i], min, max) & 0x3) << 4) |
57  ((int)(compressToByte(data[i + 1], min, max) & 0xF0) >> 4)]);
58  l.appendChar(encTable[((compressToByte(data[i + 1], min, max) & 0xF) << 2)]);
59  }
60 
61  l.appendChar('=');
62  }
63 }
64 #endif /* EFI_TEXT_LOGGING */
65 
66 void knockSpectorgramAddLine(float main_freq, float* data, size_t size) {
67 #if EFI_TEXT_LOGGING
68  if (scLogging.remainingSize() > size) {
69 
70  float min = 99999999999;
71  float max = -99999999999;
72  for(size_t i = 0; i < size; ++i) {
73  float v = data[i];
74  if(v < min) {
75  min = v;
76  }
77 
78  if(v > max) {
79  max = v;
80  }
81  }
82 
83  scLogging.reset();
84  scLogging.appendPrintf(PROTOCOL_KNOCK_SPECTROGRAMM LOG_DELIMITER);
85 
86  scLogging.appendFloat(main_freq, 2);
88 
89  scLogging.appendChar((char)size);
90 
91  base64(scLogging, data, size, min, max);
92 
93  scLogging.append(LOG_DELIMITER);
94 
96  }
97 
98 #endif /* EFI_TEXT_LOGGING */
99 }
100 
101 #endif /* KNOCK_SPECTROGRAM */
void appendChar(char c)
Definition: datalogging.h:40
void void appendFloat(float value, int precision)
Definition: datalogging.cpp:97
size_t remainingSize() const
Definition: datalogging.h:49
void reset()
void appendPrintf(const char *fmt,...) __attribute__((format(printf
Definition: datalogging.cpp:80
void knockSpectorgramAddLine(float main_freq, float *data, size_t size)
static char compressToByte(const float &v, const float &min, const float &max)
void base64(Logging &l, const float *data, size_t size, const float &min, const float &max)
static Logging scLogging("knock_spectrogram", PROTOCOL_KNOCK_SPECTROGRAMM_BUFFER, sizeof(PROTOCOL_KNOCK_SPECTROGRAMM_BUFFER))
static char PROTOCOL_KNOCK_SPECTROGRAMM_BUFFER[128] CCM_OPTIONAL
void scheduleLogging(Logging *logging)
composite packet size