LUTを用いて複数のパラメータからアウトプットを計算したいです。

Python

1def calculate_output(input1, input2, input3, lut):2 # 入力値がLUTの範囲外の場合、最も近い値を使用する3 if input1 < lut[0][0]:4 input1 = lut[0][0]5 elif input1 > lut[-1][0]:6 input1 = lut[-1][0]7 8 if input2 < lut[0][1]:9 input2 = lut[0][1]10 elif input2 > lut[-1][1]:11 input2 = lut[-1][1]12 13 if input3 < lut[0][2]:14 input3 = lut[0][2]15 elif input3 > lut[-1][2]:16 input3 = lut[-1][2]17 18 # LUT内で入力値に最も近い4つの点を見つける19 idx = 020 for i in range(len(lut)):21 if lut[i][0] >= input1 and lut[i][1] >= input2 and lut[i][2] >= input3:22 idx = i 23 break24 25 # 線形補間してoutputを計算する26 x0, y0, z0, output0 = lut[idx]27 x1, y1, z1, output1 = lut[idx + 1]28 29 output = output0 + (input1 - x0) * (output1 - output0) / (x1 - x0)30 output += (input2 - y0) * (output1 - output0) / (y1 - y0)31 output += (input3 - z0) * (output1 - output0) / (z1 - z0)32 33 return output 34 35# テスト用の入力値36input1 = 0.16337input2 = 0.29738input3 = 0.40239 40# ルックアップテーブルの定義41lut = [42 [0.175992, 0.289561, 0.424524, 0.01],43 [0.163683, 0.296413, 0.407614, 0.2],44 [0.152489, 0.303623, 0.393387, 0.4],45 [0.143289, 0.308857, 0.38057, 0.6],46 [0.13626, 0.312844, 0.367287, 0.8],47 [0.130837, 0.316597, 0.357121, 1],48 [0.125503, 0.319049, 0.347518, 1.2],49 [0.120385, 0.32324, 0.33906, 1.4],50 [0.116954, 0.324452, 0.330172, 1.6],51 [0.114296, 0.32506, 0.3231, 1.8],52 [0.110625, 0.327111, 0.316316, 2],53 [0.107602, 0.329085, 0.310861, 2.2],54 [0.104512, 0.330155, 0.304826, 2.4],55 [0.101533, 0.330934, 0.299364, 2.6],56 [0.099053, 0.333527, 0.294442, 2.8],57 [0.097248, 0.334364, 0.287901, 3],58 59]60 61# 出力の計算62result_output = calculate_output(input1, input2, input3, lut)63print("計算結果:", result_output)

コメントを投稿

0 コメント