How to generate a square wave with integer values and fixed timespan?

3 ビュー (過去 30 日間)
Samuele Bolotta
Samuele Bolotta 2021 年 4 月 6 日
編集済み: Adam Danz 2021 年 4 月 7 日
I want to generate a square wave with integer values between 0 and 5 in a timespan of 30ms.
An example:
[1 1 1 1 2 2 2 4 4 4 4 4 5 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 2 2 2 2 3 3 3 ]
I've tried different things but I can't seem to find an effective way.
  2 件のコメント
David Goodmanson
David Goodmanson 2021 年 4 月 6 日
Hi Samuele, see the 'square' function.
Adam Danz
Adam Danz 2021 年 4 月 6 日
If those are the y-values, what are the corresponding x values?

サインインしてコメントする。

採用された回答

DGM
DGM 2021 年 4 月 7 日
編集済み: DGM 2021 年 4 月 7 日
Something like this. I'm assuming you want your timestep to be a uniform 30ms.
clf
% build the coarse signal
dt=0.03;
y=[1 1 1 1 2 2 2 4 4 4 4 4 5 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 2 2 2 2 3 3 3 ];
t=linspace(0,dt*numel(y),numel(y));
plot(t,y,'--b'); hold on
% but if you need better transition times, increase the resolution
tfine=linspace(0,dt*numel(y),numel(y)*100);
yfine=interp1(t,y,tfine,'nearest');
plot(tfine,yfine,'k')
While that's simple to get better transitions by interpolation, most of the signal is constant-valued. It's kind of a waste of space to have all those intermediate samples when all you really need are the points at the transitions. If you're starting from scratch, it'd be easy enough to make the vectors as needed, but let's say you're trying to work with an existing low-resolution step signal and you want to improve it without interpolating:
clf; clc
% build the coarse signal
dt=0.03;
y=[1 1 1 1 2 2 2 4 4 4 4 4 5 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 2 2 2 2 3 3 3 ];
t=linspace(0,dt*numel(y),numel(y));
plot(t,y,'--b'); hold on
% or you could reduce it to a simple series of points
% this allows a much higher effective resolution with a minimal number of samples
tt=0.0000001; % transition time
b=[t(find(diff(y)~=0)); t(find(diff(y)~=0)+1)]; % find times at steps
b=bsxfun(@plus,mean(b,1),[-tt/2;tt/2]); % reduce rise/fall time
tp=[t(1) b(:)' t(end)];
yp=interp1(t,y,tp,'nearest');
plot(tp,yp,'k')
  • Original: 35 samples, 30ms transition time
  • Full interpolation: 3500 samples, 300us transition time
  • Edges only: 14 samples, 100ns transition time
The big point is that the number of required samples in the last case is independent of transition time.
  2 件のコメント
Adam Danz
Adam Danz 2021 年 4 月 7 日
編集済み: Adam Danz 2021 年 4 月 7 日
To get a true step with identical x values at each step,
y=[1 1 1 1 2 2 2 4 4 4 4 4 5 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 2 2 2 2 3 3 3 ];
dy = cumsum([false, diff(y(:).')==0]);
dt=0.03;
x = dy * dt;
plot(x,y)
DGM
DGM 2021 年 4 月 7 日
I didn't even stop to think that you could do that. I mean, it makes sense in retrospect. I'm just way too used to trying to build step signals in programs where you can't actually have vertical lines.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by