function [V_out, V_out_inverted, LSB] = stair_filter(V_in, V_min, V_max)
% stair_filter: 入力電圧をN段階に量子化し、その反転と1 LSBを追加
% V_in: 入力電圧 (スカラーまたはベクトル)
% V_min: 最小電圧(グラウンド電圧)
% V_max: 最大電圧(電源電圧)
% V_out: 量子化された出力
% V_out_inverted: 量子化された出力を反転させたもの
% LSB: 1 LSBの値
% Nを7に固定
N = 7;
% 各段階の幅(1 LSB)を計算
LSB = (V_max - V_min) / (N - 1);
% 入力電圧が0のときは出力も0に設定
if V_in == 0
V_out = V_min;
else
% 入力電圧をN段階に丸める
V_out = round((V_in - V_min) / LSB) * LSB + V_min;
% 出力が範囲外にならないように制限
V_out = min(max(V_out, V_min), V_max);
end
% 反転させた出力を計算
V_out_inverted = -V_out;
end
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme