1 view (last 30 days)

Hello, I'm new to matlab. I kind of like it and i have a project at university based on matlab. I'm kind of bad at programming but i want to learn.

So i have the following exercise to solve:

I need to make a square signal on multiple levels.

The period of every signal is 0.25 s and i have the following levels:

a) {-1,1}

b) {-3,-1,1,3}

c) {-5,-3,-1,1,3,5}

d){-7,-5,-3,-1,1,3,5,7}

I m sorry if this aren't the rules of the website but i'm kind of desperate. If someone can solve the d) {-7,-5,-3,-1,1,3,5,7} so i can inspire from that.

Shubham Gupta
on 1 Nov 2019

Below code worked for me :

rng default

tsim = 0:0.002:10; % simulation time

Amp = {-7,-5,-3,-1,1,3,5,7}; % Amplitudes

dt = 0.25; % sample time

randAmp = [Amp{floor(length(Amp)*rand(tsim(end)/dt,1))'+1}]; % randomize amplitude over simulation time

func = @(t)0; % initialize function

for i = 1:length(randAmp)

func = @(t)func(t)+((t-dt*(i-1))*(t-(dt-1e-10)*i)<=0)*randAmp(i); % check where 't' lies and decide amplitude accordingly

end

plot(tsim,arrayfun(func,tsim)) % plot the results

Let me know if you have doubts !

Shubham Gupta
on 3 Nov 2019

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762365

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762365

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762368

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762368

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762369

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762369

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762370

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762370

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762372

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762372

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762376

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/488618-how-can-i-solve#comment_762376

Sign in to comment.