Commit f407a506 by tatsukiishikawa

updating lab1

parent 67dbbaab
*/.DS_Store
build/
\ No newline at end of file
build
!.vscode/*
{
"configurations": [
{
"name": "Pico",
"includePath": [
"${workspaceFolder}/**",
"${userHome}/.pico-sdk/sdk/2.2.0/**"
],
"forcedInclude": [
"${userHome}/.pico-sdk/sdk/2.2.0/src/common/pico_base_headers/include/pico.h",
"${workspaceFolder}/build/generated/pico_base/pico/config_autogen.h"
],
"defines": [],
"compilerPath": "${userHome}/.pico-sdk/toolchain/14_2_Rel1/bin/arm-none-eabi-gcc",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "linux-gcc-arm"
}
],
"version": 4
}
[
{
"name": "Pico",
"compilers": {
"C": "${command:raspberry-pi-pico.getCompilerPath}",
"CXX": "${command:raspberry-pi-pico.getCxxCompilerPath}"
},
"environmentVariables": {
"PATH": "${command:raspberry-pi-pico.getEnvPath};${env:PATH}"
},
"cmakeSettings": {
"Python3_EXECUTABLE": "${command:raspberry-pi-pico.getPythonPath}"
}
}
]
\ No newline at end of file
{
"recommendations": [
"marus25.cortex-debug",
"ms-vscode.cpptools-extension-pack",
"raspberry-pi.raspberry-pi-pico",
"ms-vscode.vscode-serial-monitor",
"ms-vscode.cpptools"
]
}
\ No newline at end of file
{
"version": "0.2.0",
"configurations": [
{
"name": "Pico Debug (Cortex-Debug)",
"cwd": "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
"executable": "${command:raspberry-pi-pico.launchTargetPath}",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"serverpath": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
"gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
"device": "${command:raspberry-pi-pico.getChipUppercase}",
"configFiles": [
"interface/cmsis-dap.cfg",
"target/${command:raspberry-pi-pico.getTarget}.cfg"
],
"svdFile": "${userHome}/.pico-sdk/sdk/2.2.0/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
"runToEntryPoint": "main",
// Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
// Also works fine for flash binaries
"overrideLaunchCommands": [
"monitor reset init",
"load \"${command:raspberry-pi-pico.launchTargetPath}\""
],
"openOCDLaunchCommands": [
"adapter speed 5000"
]
},
{
"name": "Pico Debug (Cortex-Debug with external OpenOCD)",
"cwd": "${workspaceRoot}",
"executable": "${command:raspberry-pi-pico.launchTargetPath}",
"request": "launch",
"type": "cortex-debug",
"servertype": "external",
"gdbTarget": "localhost:3333",
"gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
"device": "${command:raspberry-pi-pico.getChipUppercase}",
"svdFile": "${userHome}/.pico-sdk/sdk/2.2.0/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
"runToEntryPoint": "main",
// Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
// Also works fine for flash binaries
"overrideLaunchCommands": [
"monitor reset init",
"load \"${command:raspberry-pi-pico.launchTargetPath}\""
]
},
]
}
{
"cmake.showSystemKits": false,
"cmake.options.statusBarVisibility": "hidden",
"cmake.options.advanced": {
"build": {
"statusBarVisibility": "hidden"
},
"launch": {
"statusBarVisibility": "hidden"
},
"debug": {
"statusBarVisibility": "hidden"
}
},
"cmake.configureOnEdit": true,
"cmake.automaticReconfigure": true,
"cmake.configureOnOpen": true,
"cmake.generator": "Ninja",
"cmake.cmakePath": "${userHome}/.pico-sdk/cmake/v3.31.5/bin/cmake",
"C_Cpp.debugShortcut": false,
"terminal.integrated.env.windows": {
"PICO_SDK_PATH": "${env:USERPROFILE}/.pico-sdk/sdk/2.2.0",
"PICO_TOOLCHAIN_PATH": "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1",
"Path": "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1/bin;${env:USERPROFILE}/.pico-sdk/picotool/2.2.0/picotool;${env:USERPROFILE}/.pico-sdk/cmake/v3.31.5/bin;${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1;${env:PATH}"
},
"terminal.integrated.env.osx": {
"PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.2.0",
"PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
"PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.2.0/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}"
},
"terminal.integrated.env.linux": {
"PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.2.0",
"PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
"PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.2.0/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}"
},
"raspberry-pi-pico.cmakeAutoConfigure": false,
"raspberry-pi-pico.useCmakeTools": true,
"raspberry-pi-pico.cmakePath": "${HOME}/.pico-sdk/cmake/v3.31.5/bin/cmake",
"raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja"
}
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile Project",
"type": "process",
"isBuildCommand": true,
"command": "${userHome}/.pico-sdk/ninja/v1.12.1/ninja",
"args": ["-C", "${workspaceFolder}/build"],
"group": "build",
"presentation": {
"reveal": "always",
"panel": "dedicated"
},
"problemMatcher": "$gcc",
"windows": {
"command": "${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1/ninja.exe"
}
},
{
"label": "Run Project",
"type": "process",
"command": "${env:HOME}/.pico-sdk/picotool/2.2.0/picotool/picotool",
"args": [
"load",
"${command:raspberry-pi-pico.launchTargetPath}",
"-fx"
],
"presentation": {
"reveal": "always",
"panel": "dedicated"
},
"problemMatcher": [],
"windows": {
"command": "${env:USERPROFILE}/.pico-sdk/picotool/2.2.0/picotool/picotool.exe"
}
},
{
"label": "Flash",
"type": "process",
"command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
"args": [
"-s",
"${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/${command:raspberry-pi-pico.getTarget}.cfg",
"-c",
"adapter speed 5000; program \"${command:raspberry-pi-pico.launchTargetPath}\" verify reset exit"
],
"problemMatcher": [],
"windows": {
"command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
}
},
{
"label": "Rescue Reset",
"type": "process",
"command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
"args": [
"-s",
"${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/${command:raspberry-pi-pico.getChip}-rescue.cfg",
"-c",
"adapter speed 5000; reset halt; exit"
],
"problemMatcher": [],
"windows": {
"command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
}
},
{
"label": "Risc-V Reset (RP2350)",
"type": "process",
"command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
"args": [
"-s",
"${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
"-c",
"set USE_CORE { rv0 rv1 cm0 cm1 }",
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/rp2350.cfg",
"-c",
"adapter speed 5000; init;",
"-c",
"write_memory 0x40120158 8 { 0x3 }; echo [format \"Info : ARCHSEL 0x%02x\" [read_memory 0x40120158 8 1]];",
"-c",
"reset halt; targets rp2350.rv0; echo [format \"Info : ARCHSEL_STATUS 0x%02x\" [read_memory 0x4012015C 8 1]]; exit"
],
"problemMatcher": [],
"windows": {
"command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
}
}
]
}
...@@ -21,13 +21,13 @@ pico_sdk_init() ...@@ -21,13 +21,13 @@ pico_sdk_init()
add_executable(main add_executable(main
#quantization.c #quantization.c
#fft.c #fft.c
sobel.c #sobel.c
#statistic.c statistic.c
) )
pico_enable_stdio_usb(main 1) pico_enable_stdio_usb(main 1)
pico_enable_stdio_uart(main 1) pico_enable_stdio_uart(main 1)
#pico_add_extra_outputs() pico_add_extra_outputs(main)
# Pull in our pico_stdlib which pulls in commonly used features # Pull in our pico_stdlib which pulls in commonly used features
target_link_libraries(main pico_stdlib m) target_link_libraries(main pico_stdlib m)
...@@ -155,6 +155,22 @@ int main(void){ ...@@ -155,6 +155,22 @@ int main(void){
i+1, idx[i], fk, val[i]); i+1, idx[i], fk, val[i]);
} }
printf("\n");
printf("TO COPY ONTO A TEXT FILE FOR COMPARSION WITH PC RUN FFT\n");
// Print the raw values onto the serial in one line
for (int n = 0; n < N; n++) { if (n) printf(","); printf("%.6f", (double)x[n]); } printf("\n");
printf("\n");
printf("TO COPY ONTO A TEXT FILE FOR COMPARSION WITH PC RUN FFT\n");
// Print to the serial the output of the fft to compare with the host run python implmentation of the fft filter
printf("bin,freq,amp\n");
for(int i=0;i<found;i++)
{
float fk = df * (float)idx[i];
printf("%d,%.6f,%.6f\n", idx[i], fk, val[i]);
}
while(true) tight_loop_contents(); while(true) tight_loop_contents();
return 0; return 0;
} }
...@@ -3,11 +3,28 @@ import matplotlib.pyplot as plt ...@@ -3,11 +3,28 @@ import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
from math import pi from math import pi
TEST_MODE = False
TEST_MODE_ADD_NOISE_TO_IMU = True
fs = 2200.0 fs = 2200.0
N = 256 N = 256
t = np.arange(N) / fs t = np.arange(N) / fs
x = 0.7*np.sin(2*pi*120.0*t) + 0.3*np.sin(2*pi*440.0*t) if TEST_MODE:
x = 0.7*np.sin(2*pi*120.0*t) + 0.3*np.sin(2*pi*440.0*t)
if TEST_MODE_ADD_NOISE_TO_IMU:
imu_noisy = lambda x,t,seed=None: (lambda r,dt: (1+r.normal(0,2e-1))*(x + r.normal(0,2e-3/np.sqrt(2*dt),x.shape) + np.cumsum(r.normal(0,5e-5,x.shape))*dt))(np.random.default_rng(seed), float(np.mean(np.diff(t))))
x = imu_noisy(x, t, seed=5)
else:
# Read data from a txt file. (saved from output of mcu)
with open("raw_imu_mcu.txt", "r") as f:
data = f.readline()
x = []
data = data.split(",")
for val in data:
x.append(float(val))
x = np.array(x)
w = np.hamming(N) w = np.hamming(N)
xw = x * w xw = x * w
...@@ -52,5 +69,32 @@ spec_png = "imu_fft_spectrum.png" ...@@ -52,5 +69,32 @@ spec_png = "imu_fft_spectrum.png"
plt.savefig(spec_png, dpi=150) plt.savefig(spec_png, dpi=150)
df = pd.DataFrame({"bin": top_indices, "freq_hz": top_freqs, "amplitude": top_amps}) df = pd.DataFrame({"bin": top_indices, "freq_hz": top_freqs, "amplitude": top_amps})
print("Output of FFT on PC")
print(df)
def compare_output(pc_bin, pc_freq, pc_ampli):
df_mcu = pd.read_csv(
"out_fft_mcu.txt",
header=None,
names=["bin", "mcu_freq", "mcu_amp"],
skiprows=1,
).apply(pd.to_numeric, errors="coerce").dropna()
df_pc = pd.DataFrame(
{"bin": pc_bin, "pc_freq": pc_freq, "pc_amp": pc_ampli}
).apply(pd.to_numeric, errors="coerce").dropna()
df = pd.merge(df_mcu, df_pc, on="bin", how="inner", sort=False)
print("MCU BIN; PC BIN; MCU FREQ; PC FREQ; MCU AMP; PC AMP")
for _, r in df.iterrows():
print(f"{r['bin']}; {r['bin']}; {r['mcu_freq']}; {r['pc_freq']}; {r['mcu_amp']}; {r['pc_amp']}")
is_all_freq_correct = bool((df["mcu_freq"] == df["pc_freq"]).all())
print("All frequencies match: ", is_all_freq_correct)
time_png, spec_png, df if not TEST_MODE:
compare_output(top_indices, top_freqs, top_amps)
\ No newline at end of file
bin,freq,amp
14,120.312500,0.697141
13,111.718750,0.318609
51,438.281250,0.289742
15,128.906250,0.278794
52,446.875000,0.176296
\ No newline at end of file
0.000000,0.041717,0.049901,0.034380,0.032954,0.057846,0.076892,0.056407,0.008549,-0.022392,-0.018517,-0.012209,-0.040134,-0.088233,-0.105251,-0.070718,-0.024980,-0.015872,-0.031890,-0.016416,0.050990,0.117735,0.122898,0.079508,0.058026,0.087051,0.107783,0.052663,-0.054765,-0.121200,-0.105835,-0.076258,-0.112911,-0.188516,-0.194966,-0.091099,0.031618,0.066155,0.035427,0.059920,0.182052,0.290340,0.260362,0.126441,0.040978,0.066058,0.084689,-0.034182,-0.237730,-0.340491,-0.268223,-0.159435,-0.172914,-0.259903,-0.224123,0.000001,0.235649,0.287326,0.200996,0.194874,0.344745,0.460218,0.337933,0.051105,-0.133187,-0.109288,-0.071327,-0.231582,-0.501834,-0.589001,-0.388770,-0.134715,-0.083867,-0.164932,-0.083026,0.252004,0.568230,0.578949,0.365432,0.260125,0.380527,0.459358,0.218803,-0.221806,-0.478516,-0.407348,-0.286157,-0.413126,-0.672644,-0.678506,-0.309275,0.104735,0.213857,0.111789,0.184606,0.547740,0.853282,0.747611,0.354817,0.112404,0.177173,0.222138,-0.087707,-0.596840,-0.836601,-0.645139,-0.375474,-0.398801,-0.587181,-0.496101,0.000002,0.501033,0.598917,0.410819,0.390635,0.677875,0.887830,0.639712,0.094949,-0.242889,-0.195670,-0.125391,-0.399800,-0.850927,-0.981069,-0.636187,-0.216606,-0.132515,-0.256121,-0.126729,0.378120,0.838213,0.839700,0.521175,0.364827,0.524881,0.623203,0.291991,-0.291172,-0.617984,-0.517575,-0.357735,-0.508174,-0.814165,-0.808167,-0.362516,0.120812,0.242790,0.124905,0.203015,0.592870,0.909057,0.783959,0.366223,0.114198,0.177171,0.218651,-0.084975,-0.569165,-0.785273,-0.596030,-0.341428,-0.356918,-0.517208,-0.430063,0.000003,0.420650,0.494808,0.333979,0.312476,0.533515,0.687463,0.487309,0.071151,-0.179032,-0.141858,-0.089404,-0.280329,-0.586692,-0.665073,-0.424002,-0.141909,-0.085335,-0.162093,-0.078817,0.231066,0.503235,0.495211,0.301884,0.207528,0.293168,0.341731,0.157167,-0.153813,-0.320340,-0.263220,-0.178461,-0.248627,-0.390592,-0.380101,-0.167119,0.054581,0.107465,0.054157,0.086205,0.246493,0.369980,0.312264,0.142728,0.043537,0.066056,0.079710,-0.030280,-0.198216,-0.267199,-0.198102,-0.110821,-0.113109,-0.159995,-0.129832,0.000001,0.120872,0.138677,0.091279,0.083271,0.138614,0.174122,0.120319,0.017125,-0.042008,-0.032452,-0.019943,-0.060992,-0.124542,-0.137800,-0.085791,-0.028059,-0.016500,-0.030676,-0.014615,0.042031,0.089926,0.087069,0.052319,0.035522,0.049671,0.057454,0.026291,-0.025674,-0.053521,-0.044167,-0.030178,-0.042528,-0.067836,-0.067289,-0.030276
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#include "pico/stdlib.h" #include "pico/stdlib.h"
#define N 64 // number of samples #define N 64 // number of samples
#define FS 2200.0f // sample rate #define FS 2200.0f // sample rate
// ---------- small helpers ---------- // ---------- small helpers (statistics) ----------
// arithmetic mean
static float mean_f32(const float* x, int n) { static float mean_f32(const float* x, int n) {
double acc = 0.0; double acc = 0.0;
for (int i = 0; i < n; i++) acc += x[i]; for (int i = 0; i < n; i++) acc += x[i];
return (float)(acc / (double)n); return (float)(acc / (double)n);
} }
// sample variance (denominator n-1)
static float variance_f32(const float* x, int n, float mean) { static float variance_f32(const float* x, int n, float mean) {
// sample variance (n-1)
if (n <= 1) return 0.0f; if (n <= 1) return 0.0f;
double acc = 0.0; double acc = 0.0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
...@@ -24,6 +27,12 @@ static float variance_f32(const float* x, int n, float mean) { ...@@ -24,6 +27,12 @@ static float variance_f32(const float* x, int n, float mean) {
return (float)(acc / (double)(n - 1)); return (float)(acc / (double)(n - 1));
} }
// standard deviation from variance
static float stddev_f32(float variance) {
return sqrtf(variance);
}
// min & max
static void min_max_f32(const float* x, int n, float* mn, float* mx) { static void min_max_f32(const float* x, int n, float* mn, float* mx) {
float a = x[0], b = x[0]; float a = x[0], b = x[0];
for (int i = 1; i < n; i++) { for (int i = 1; i < n; i++) {
...@@ -33,28 +42,45 @@ static void min_max_f32(const float* x, int n, float* mn, float* mx) { ...@@ -33,28 +42,45 @@ static void min_max_f32(const float* x, int n, float* mn, float* mx) {
*mn = a; *mx = b; *mn = a; *mx = b;
} }
static void min_max_scale_inplace(float* x, int n, float a, float b) { // median (sorts a local copy; insertion sort — fine for small N)
float mn, mx; min_max_f32(x, n, &mn, &mx); static float median_f32(const float* x, int n) {
float range = mx - mn; float tmp[n];
if (range == 0.0f) { for (int i = 0; i < n; i++) tmp[i] = x[i];
float mid = 0.5f * (a + b);
for (int i = 0; i < n; i++) x[i] = mid; // insertion sort (ascending)
return; for (int i = 1; i < n; i++) {
float key = tmp[i];
int j = i - 1;
while (j >= 0 && tmp[j] > key) {
tmp[j + 1] = tmp[j];
j--;
} }
for (int i = 0; i < n; i++) { tmp[j + 1] = key;
float t = (x[i] - mn) / range;
x[i] = a + t * (b - a);
} }
}
static void zscore_inplace(float* x, int n, float mean, float var) { if (n & 1) return tmp[n / 2];
float std = sqrtf(var); return 0.5f * (tmp[n / 2 - 1] + tmp[n / 2]);
if (std == 0.0f) { for (int i = 0; i < n; i++) x[i] = 0.0f; return; }
for (int i = 0; i < n; i++) x[i] = (x[i] - mean) / std;
} }
static void copy_vec(const float* src, float* dst, int n) { // mode (for discrete/repeated values). If all counts are 1, no mode.
for (int i = 0; i < n; i++) dst[i] = src[i]; // eps lets you treat near-equal floats as equal (use 0 for exact).
static float mode_f32(const float* x, int n, int* count_out, float eps) {
int best_count = 0;
float best_val = NAN;
for (int i = 0; i < n; i++) {
int cnt = 1;
for (int j = i + 1; j < n; j++) {
if (fabsf(x[j] - x[i]) <= eps) cnt++;
}
if (cnt > best_count) {
best_count = cnt;
best_val = x[i];
}
}
if (count_out) *count_out = best_count;
return best_val; // if best_count==1, there is no mode
} }
int main(void) { int main(void) {
...@@ -71,10 +97,7 @@ int main(void) { ...@@ -71,10 +97,7 @@ int main(void) {
+ 0.10f * sinf(2.0f * (float)M_PI * 7.0f * t); // mix + 0.10f * sinf(2.0f * (float)M_PI * 7.0f * t); // mix
} }
// ---- stats per axis ---- // ---- statistics per axis ----
float mx, vx, mny, vny, mnz, vnz;
float mnx, Mx, mny_min, mny_max, mnz_min, mnz_max;
float meanx = mean_f32(ax, N); float meanx = mean_f32(ax, N);
float meany = mean_f32(ay, N); float meany = mean_f32(ay, N);
float meanz = mean_f32(az, N); float meanz = mean_f32(az, N);
...@@ -83,57 +106,48 @@ int main(void) { ...@@ -83,57 +106,48 @@ int main(void) {
float vary = variance_f32(ay, N, meany); float vary = variance_f32(ay, N, meany);
float varz = variance_f32(az, N, meanz); float varz = variance_f32(az, N, meanz);
float stdx = stddev_f32(varx);
float stdy = stddev_f32(vary);
float stdz = stddev_f32(varz);
float medx = median_f32(ax, N);
float medy = median_f32(ay, N);
float medz = median_f32(az, N);
int mcount_x, mcount_y, mcount_z;
// eps=0.0f -> exact repeats only. Use e.g. 1e-4f if you want near-equality.
float modex = mode_f32(ax, N, &mcount_x, 0.0f);
float modey = mode_f32(ay, N, &mcount_y, 0.0f);
float modez = mode_f32(az, N, &mcount_z, 0.0f);
float mnx, Mx, mny, My, mnz, Mz;
min_max_f32(ax, N, &mnx, &Mx); min_max_f32(ax, N, &mnx, &Mx);
min_max_f32(ay, N, &mny_min, &mny_max); min_max_f32(ay, N, &mny, &My);
min_max_f32(az, N, &mnz_min, &mnz_max); min_max_f32(az, N, &mnz, &Mz);
printf("=== IMU Normalization Demo ===\n"); printf("=== IMU Statistics Lab ===\n");
printf("N=%d, fs=%.1f Hz\n\n", N, FS); printf("N=%d, fs=%.1f Hz\n\n", N, FS);
printf("Raw stats per-axis:\n"); printf("Raw stats per-axis:\n");
printf(" ax: mean=% .5f var=% .5f min=% .5f max=% .5f\n", meanx, varx, mnx, Mx); printf(" ax: mean=% .5f median=% .5f var=% .5f std=% .5f min=% .5f max=% .5f\n",
printf(" ay: mean=% .5f var=% .5f min=% .5f max=% .5f\n", meany, vary, mny_min, mny_max); meanx, medx, varx, stdx, mnx, Mx);
printf(" az: mean=% .5f var=% .5f min=% .5f max=% .5f\n\n", meanz, varz, mnz_min, mnz_max); if (mcount_x > 1) printf(" mode=% .5f (count=%d)\n", modex, mcount_x);
else printf(" mode: none (no repeated values)\n");
// ---- Min-Max to [0,1] (per axis) ----
float ax_mm[N], ay_mm[N], az_mm[N]; printf(" ay: mean=% .5f median=% .5f var=% .5f std=% .5f min=% .5f max=% .5f\n",
copy_vec(ax, ax_mm, N); copy_vec(ay, ay_mm, N); copy_vec(az, az_mm, N); meany, medy, vary, stdy, mny, My);
min_max_scale_inplace(ax_mm, N, 0.0f, 1.0f); if (mcount_y > 1) printf(" mode=% .5f (count=%d)\n", modey, mcount_y);
min_max_scale_inplace(ay_mm, N, 0.0f, 1.0f); else printf(" mode: none (no repeated values)\n");
min_max_scale_inplace(az_mm, N, 0.0f, 1.0f);
printf(" az: mean=% .5f median=% .5f var=% .5f std=% .5f min=% .5f max=% .5f\n",
float axmm_min, axmm_max, aymm_min, aymm_max, azmm_min, azmm_max; meanz, medz, varz, stdz, mnz, Mz);
min_max_f32(ax_mm, N, &axmm_min, &axmm_max); if (mcount_z > 1) printf(" mode=% .5f (count=%d)\n", modez, mcount_z);
min_max_f32(ay_mm, N, &aymm_min, &aymm_max); else printf(" mode: none (no repeated values)\n");
min_max_f32(az_mm, N, &azmm_min, &azmm_max);
// ---- optional: peek first 10 samples ----
printf("After Min-Max scaling to [0,1]:\n"); printf("\nFirst 10 samples (ax, ay, az):\n");
printf(" ax_mm: min=%.3f max=%.3f\n", axmm_min, axmm_max);
printf(" ay_mm: min=%.3f max=%.3f\n", aymm_min, aymm_max);
printf(" az_mm: min=%.3f max=%.3f\n\n", azmm_min, azmm_max);
// ---- Z-Score (per axis) ----
float ax_zs[N], ay_zs[N], az_zs[N];
copy_vec(ax, ax_zs, N); copy_vec(ay, ay_zs, N); copy_vec(az, az_zs, N);
zscore_inplace(ax_zs, N, meanx, varx);
zscore_inplace(ay_zs, N, meany, vary);
zscore_inplace(az_zs, N, meanz, varz);
float mx2 = mean_f32(ax_zs, N), vx2 = variance_f32(ax_zs, N, mx2);
float my2 = mean_f32(ay_zs, N), vy2 = variance_f32(ay_zs, N, my2);
float mz2 = mean_f32(az_zs, N), vz2 = variance_f32(az_zs, N, mz2);
printf("After Z-Score (should be ~mean=0, var=1):\n");
printf(" ax_zs: mean=% .3f var=% .3f\n", mx2, vx2);
printf(" ay_zs: mean=% .3f var=% .3f\n", my2, vy2);
printf(" az_zs: mean=% .3f var=% .3f\n\n", mz2, vz2);
// ---- Show first 10 samples (orig -> minmax -> zscore) ----
printf("idx | ax ay az || ax_mm ay_mm az_mm || ax_zs ay_zs az_zs\n");
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
printf("%3d | % .5f % .5f % .5f || %.3f %.3f %.3f || % .3f % .3f % .3f\n", printf("%3d | % .5f % .5f % .5f\n", i, ax[i], ay[i], az[i]);
i, ax[i], ay[i], az[i],
ax_mm[i], ay_mm[i], az_mm[i],
ax_zs[i], ay_zs[i], az_zs[i]);
} }
while (true) tight_loop_contents(); while (true) tight_loop_contents();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment