logo elektroda
logo elektroda
X
logo elektroda

Modifying Duration, Amplitude and Periods of Rectangular Signal in Matlab Easily

Hunter-Killer 19489 23
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 10130644
    Hunter-Killer
    Level 9  
    For some time now I have a problem with generating a rectangular signal in matlab - but in such a way that it is easy to change its duration, amplitude and number of displayed periods.
    For example, for sine there was no problem:

    y=A*sin(2*w*m);

    Pomerania anyone? :)
  • ADVERTISEMENT
  • #3 10130772
    Hunter-Killer
    Level 9  
    A sine with few harmonics ? How can I transfer this to matlab? I can transfer the functions you provided on average - at least in ways known to me (zeros, ones).
  • ADVERTISEMENT
  • #4 10130785
    kuuczoo
    Level 26  
    I was just using the sine function. I don't remember the exact syntax but something like if (sin(2*w*m))>=0 then y=1 else y=-1.
  • #5 10130830
    Hunter-Killer
    Level 9  
    In this example of mine with the sine of the notation:

    A - amplitude
    m=T:0.0001:TE;
    T - starting time
    TE - end time
    w=number_periods*pi/TE;

    so I guess I won't do it like you said :/

    I wanted to do this:

    t=-5:0.0001:5;
    pulse = A*rectpulse(t,3);
    plot(t,pulse)

    but in this case I can't give myself how many periods there will be - there is always one and it's in the middle of the axis :/
  • #6 10130924
    kuuczoo
    Level 26  
    Why not? You have already set all parameters of the waveform in the sine signal. The if condition only changes the shape. The only downside of the solution is that "1" is longer than "-1" by 2 samples.
  • #7 10130964
    Hunter-Killer
    Level 9  
    And can you write it all down somehow - I don't think I've ever done it like that and therefore I don't know how to "bite it"? :)
  • #8 10131010
    kuuczoo
    Level 26  
    Not to mislead you. I was dealing with matlab about 5 years ago and I don't remember the syntax. Build an array with the time and values of the sine function since at specified w, then overwrite it with a condition to the same array.
    Assuming you already have y=sin... something like this:
    for k=1:length(y),
    if (y(k) >=0) y(k)=A,
    else y(k)=-1*A,
    end
    end

    Try it and then fix what doesn't work

    -------------------

    I checked on Octave. Works
  • #9 10131117
    Hunter-Killer
    Level 9  
    It will be somehow different than before - Matlab is not C++ :)
  • #10 10131121
    kuuczoo
    Level 26  
    I checked the code above on Octave - it went and generated a nice rectangle.
  • #11 10131137
    Hunter-Killer
    Level 9  
    It's not possible - it throws errors in elsa for me :/

    Paste all the code you used :)
  • ADVERTISEMENT
  • #12 10131153
    kuuczoo
    Level 26  
    for example:
    t=0:0.01:6;
    w=5 * pi / 6.0;
    y=sin (2 * w * t);
    for k=1:length(y),
    if (y(k) >=0) y(k)=1,
    else y(k)=-1,
    end
    end

    fences)
    hold on

    And it works :)
  • #13 10131205
    Hunter-Killer
    Level 9  
    It actually works - in elsa I gave ; instead ,... :)
    But how to determine the length of the period here?

    Actually, there's something about the scale because it's all times 100 :/
  • #14 10131237
    kuuczoo
    Level 26  
    What do you mean by period length?
    You set the time range at the beginning and then when counting "w" you specify the number of periods, so the length of the period is TE/n.
  • #15 10131253
    Hunter-Killer
    Level 9  
    Yes, that's right - but I'm talking about the scale - I don't know why there are such large values since:

    t=0:0.01:6;

    then max should be 6 in my opinion - and here 600 is ...
  • #16 10131273
    kuuczoo
    Level 26  
    Are you displaying it as I wrote? (plot (y)) If so, then it should be, because the x-axis is the sample number in the table, and since there are 601 of them, these are the x-scale values. Try plot(t,y)...
  • ADVERTISEMENT
  • #17 10131364
    Hunter-Killer
    Level 9  
    That's right - I don't know why I didn't try it myself. anyway thanks - now everything is as I wanted :) . I generated the sawtooth signal myself so I got what I wanted - thanks :) .
  • #18 10131882
    Elektronik9
    Level 30  
    Hunter-Killer wrote:
    A sine with few harmonics ? How can I transfer this to matlab? I can transfer the functions you provided on average - at least in ways known to me (zeros, ones).

    In Matlab, a function named heaviside , properly combining the signal from these functions and "looping" it so that it is periodic, you will get a square wave.
  • #19 10132288
    piotrunio11
    Level 11  
    Sometimes gentlemen, it's worth using the ''symulink'' overlay And if something doesn't work out or comes out poorly, give up electronics and electrical engineering in mat. After all, there are intuitive programs like Tcad.
  • #20 10133583
    Hunter-Killer
    Level 9  
    Just what I do works well in matlab - I had a problem with this rectangle only and with one more thing I have a problem...

    Is it possible to make a script in matlab such as in the picture below:

    Modifying Duration, Amplitude and Periods of Rectangular Signal in Matlab Easily

    I just have a few graphs on one to make a comparison, but the most is happening around, for example, the value of 8 on the Y scale and I would like it to be "denser" with values such as changes every 0.1 and then larger changes - it can be done somehow and it's so that matlab itself would set it (automatically) and I wouldn't have to set every graph myself.
    I thought it was a logarithmic scale in matlab and after the case, but calling semilogy does not do the job .... some ideas (actually not this topic, but related :) ).
  • #21 10133657
    Elektronik9
    Level 30  
    Maybe it's enough just to enlarge (zoom) the graph window?
  • #22 10133666
    Hunter-Killer
    Level 9  
    If I zoom in then I can't see the whole graph...

    Maybe something with the axis min and max to combine and hold on to show several graphs, matlab itself will determine the scale?
  • #23 10136369
    mungo
    Level 19  
    I'm not very good with matlab but sometimes there was no such thing as "square" to generate a square wave??
  • #24 10137637
    Hunter-Killer
    Level 9  
    Yes, it is to be generated, but then there is a problem with determining the period of the signal - in any case, "kuuczoo" proposed the most suitable solution for me, so I think the topic is already closed - thanks to all those who helped.

Topic summary

The discussion revolves around generating a rectangular signal in MATLAB, focusing on modifying its duration, amplitude, and number of displayed periods. Users suggest using the Heaviside step function and conditional statements to create the rectangular waveform. A common approach involves using a sine function to define the signal and then applying a conditional loop to convert it into a rectangular shape. The importance of correctly setting the time range and understanding the period length is emphasized. Additionally, there are inquiries about adjusting graph scales and displaying multiple graphs effectively. The conversation concludes with a user expressing satisfaction with the solutions provided.
Summary generated by the language model.
ADVERTISEMENT