Signal generation using for loop

19 vues (au cours des 30 derniers jours)
Krishnendu K
Krishnendu K le 17 Jan 2016
How to generate a composite signal in matlab? In my case i want to generate and plot a signal which has values t+1 for t=-2:-1 & 1 for t=-1:0 & 2 for t=0:1 & 2-t for t=1:2 using for loop.. Pls help
  2 commentaires
jgg
jgg le 18 Jan 2016
Can you explain more clearly. Your loop does not make sense: as written for t=-1 the output needs to take on both the value 0 and 1.
Krishnendu K
Krishnendu K le 18 Jan 2016
My signal looks like this..

Connectez-vous pour commenter.

Réponse acceptée

Mohazam Awan
Mohazam Awan le 2 Avr 2017
Here's an example

Plus de réponses (3)

Titus Edelhofer
Titus Edelhofer le 18 Jan 2016
Hi, what about this:
x = [-2 -2 -1 -1 0 0 1 1 2];
y = [ 0 -1 0 1 1 2 2 1 0];
plot(x,y)
Titus
  1 commentaire
Krishnendu K
Krishnendu K le 18 Jan 2016
sir can u use loops for this query pls..

Connectez-vous pour commenter.


John BG
John BG le 18 Jan 2016
1.- generating base pulse with SIMULINK:
1.1.- manually following, one way to generate the base pulse of the cyclic signal you are after
var.time=[0 1 1 2 2 3 3 4 4 5]
var.signals.dimensions=1
var.signals.values=[-1 0 1 1 2 2 1 0 0 0]'
Open SIMULINK and build the following circuit:
change Scope parameters, tab: history, tick 'Save data to workspace' (default scope output structure name: ScopeData) run simulation again. now the base pulse data is available in:
y=ScopeData.time
ny=ScopeData.signals.values
y is the signal base pulse and ny is the time reference.
Remove time and signal values > t=4
y(find(ny>4))=[];ny(find(ny>4))=[]
figure(1);plot(ny,y)
just to read horizontally:
y=y'
ny=ny'
instead of for loop, you can also use repmat. 20 cycles would be:
y20=repmat(y,1,20)
ny20=repmat(ny,1,20)
you can center the signal to have the base pulse phase just as you sketched in the question, with ny=0 (t=0) right on the middle of the base pulse, right on the 1 to 2 step up with:
ny=ny-2
1.2.- as expected, SIMULINK already has a block to generate periodic signals, in the Library Browser>Sources use the block called 'Repeating Sequences' and edit the signal and time reference as done above manually.
2.- with pulstran
the base pulse y has 28 samples, and the time reference of the base pulse is ny
y_length=length(y)
the comb reference to repeat let's say 5 times, full duty cycle, the base pulse, the 'loop' you are after is
comb=[0 1 2 3 4]*y_length
pulstran needs a time reference
ny_rep=[1:1:max(comb)]
y_rep=pulstran(ny_rep,comb,y)
figure(2);plot(ny_rep,y_rep);grid on
Let's replicate the base pulse with a low duty cycle
comb=[0 1 2 3 4]*y_length/.1
ny_rep=[1:1:max(comb)+y_length]
y_rep=pulstran(ny_rep,comb,y)
figure(3);plot(ny_rep,y_rep);grid on
use stem instead of plot to see avoid slopes where there should be sharp transition
3.- with repmat
y_rep=repmat(y,1,5)
ny_rep=[1:1:length(y_rep)]
I keep the time reference notation as n(signal_name) following notation explained in book 'Digital Signal Processing using MATLAB' by Vinay K Ingle and John G Proakis, highly recommended
You insist to Mr Edelhofer to use loops, but it's like wanting to walk 10 miles when you can drive. Once is ok, for sport, but you are learning MATLAB to solve problems, yours or some one else's, and problems need be solved on time to get paid. Now you are at school or uni, so it's MATLAB is your car but you don't have to pay gas or even MOT.
If you are programming in C, then you have no other way to engage sometimes really complicated loops to end up doing what the functions above explained do.
Really, use repmat, pulstran or SIMULINK instead of for/while loops. If your teacher insists then, very politely, don't forget the 'with all due respect' tell your teacher you all should stop using MATLAB and use TurboC, not recommended unless you are programming a PIC that has no interface for MATLAB. Hope it helps,

Mohazam Awan
Mohazam Awan le 2 Avr 2017
out put

Community Treasure Hunt

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

Start Hunting!

Translated by