Signal generation using for loop
14 次查看(过去 30 天)
显示 更早的评论
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 个评论
jgg
2016-1-18
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.
采纳的回答
更多回答(3 个)
Titus Edelhofer
2016-1-18
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
John BG
2016-1-18
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,
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!