向下移动信号

时间:2011-10-27 20:54:52

标签: matlab octave

对于方形,矩形和y轴上的锯齿信号,我有一个从0到1的信号,我怎样才能将信号向下移动(垂直偏移),这样信号将从-0.5变为0.5 y轴,并将三角形信号从0.5更改为1.0到-0.5到0.5?

clear all
% SCRIPT BEGINS
t=linspace(0,1,22050)
freq=5%how many times it repeats in 1 sec
A = 1; % amplitude
T = 1/freq; % period of the signal

% square
square = mod(t * A / T, A) > A / 2;
plot(t, square)
title('Square');

% rectangle
l = 0.2; % percentage the signal spends on low value
rectangle = mod(t * A / T, A) > A * l;
figure;
plot(t, rectangle);
title('Rectangle');

% sawtooth
sawtooth = mod(t * A / T, A);
figure;
plot(t, sawtooth);
title('Sawtooth');

% triangle
triangle = (mod(t * A / T, A) > 0.5).*mod(t * A / T, A) + (mod(t * A / T, A) <= 0.5).*(1 - mod(t * A / T, A));
figure;
plot(t, triangle);
title('triangle');

谢谢我正在使用octave / matlab

2 个答案:

答案 0 :(得分:0)

function x_new = rescale(x, new_min, new_max)

x_min = min(x);
x_max = max(x);

x_new = new_min + (new_max - new_min) * (x - x_min) / (x_max - x_min);

(顺便说一句,这是非常基本的数学,可以很容易用Google搜索)

答案 1 :(得分:0)

以下是代码,以防下一个人

clear all
% SCRIPT BEGINS
t=linspace(0,1,22050);
freq=5; %how many in 1 sec
%t = 0:0.01:1; %time vector
A = 1; % amplitude
T = 1/freq; % period of the signal

vertoffset=0.5;
% square
square = mod(t * A / T, A) > A / 2;
square = square - vertoffset;
plot(t, square)
title('Square');

% rectangle
l = 0.2; % percentage the signal spends on low value
rectangle = mod(t * A / T, A) > A * l;
rectangle = rectangle - vertoffset;
figure;
plot(t, rectangle);
title('Rectangle');

% sawtooth
sawtooth = mod(t * A / T, A);
sawtooth = sawtooth -vertoffset;
figure;
plot(t, sawtooth);
title('Sawtooth');

% triangle
triangle = (mod(t * A / T, A) > 0.5).*mod(t * A / T, A) + (mod(t * A / T, A) <= 0.5).*(1 - mod(t * A / T, A));
triangle = 2*triangle - 1.5;
figure;
plot(t, triangle);
title('triangle');
相关问题