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.

6 Comments

darova
darova on 31 Oct 2019
Can you make a simple drawing of result you expect?
Liviu Iftime
Liviu Iftime on 31 Oct 2019
The problem says is the signal is random.
Liviu Iftime
Liviu Iftime on 31 Oct 2019
t= 0:0.002:5;
n=125;% n=0.25/0.002
v=randn(1,20);k=1;s=[0 0];
for p=1:length(v)
if v(1,p)<0
v(1,p)=-1;
else v(1,p)=1;
end
end
for i=1:20
for j=1:125
s(1,k) = v(1,i);
k=k+1;
end
end
s(1,k)=v(1,i);
plot(t,s)
I have done this, and i think it s ok, but i m stucked at b)
darova
darova on 31 Oct 2019
  • I need to make a square signal on multiple levels.
What does it mean? HOw it should look like?
Liviu Iftime
Liviu Iftime on 31 Oct 2019
sp.png
Liviu Iftime
Liviu Iftime on 31 Oct 2019
WhatsApp Image 2019-11-01 at 00.55.44.jpeg

Sign in to comment.

 Accepted Answer

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 !

2 Comments

Well Thank you! I'm kind of confussed on what is that. I have managed to make myself the code and it has 140 lines. It's funny that you have in your code 10 lines, but as i said I have no ideea what is that code.
THANK YOU ANYWAYS!
I am glad I could help. If you can tell me which part of the code you didn't understand, maybe I can help you understand in more detail. Also if it solves the problem for you, you can consider to accept the answer cheers !

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!