Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc
- clear all
- % Data
- start = 0;
- finish = 4*pi;
- x = linspace(start, finish, 100);
- y = sin(x);
- MIN = min(y);
- MAX = max(y);
- % Levels
- R = 4;
- num_levels = 2 ^ R;
- differ = (MAX - MIN) / num_levels;
- levels = zeros(1, num_levels);
- for i = 1 : num_levels
- levels(i) = MIN + (2*i - 1) * differ / 2;
- end
- % Searching 'y' to snap the values
- multiplier = 3; % Must be an integer!
- LEN = round(multiplier * length(y));
- q = zeros(1, LEN);
- t = zeros(1, LEN);
- for i = 1 : LEN
- pos = round(i / multiplier);
- if pos == 0
- value = y(1);
- t(i) = x(1);
- else
- value = y(pos);
- t(i) = x(pos);
- end
- index = -1;
- d = MAX - MIN;
- for j = 1 : length(levels)
- level = levels(j);
- if abs(level-value) <= d
- d = abs(level-value);
- index = j;
- end
- end
- q(i) = levels(index);
- end
- % PLOTS
- levels
- plot(x, y);
- hold on
- plot(t, q);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement