Re: Sine wave code
Sergei Steshenko via Help-octave <[email protected]>
| Newsgroups | gmane.comp.gnu.octave.general |
|---|---|
| Message-ID | <[email protected]> |
On 29/12/2020 19:36, Ian McCallion wrote: > On Tue, 29 Dec 2020 at 14:03, Blaz <[email protected]> wrote: >> Hello coders >> >> I have a request for some sine wave code. I need to create a modified sine >> wave where I cuttof the wave at certain value and hold this until the next >> zero crossing (Please refer to the red marked line on the picture). The sine >> wave (or some other for that matter) comes in as an input vector with 20 >> samples per period (this can change though from 20 to up to 40 max, but I >> don't think this has any consequence though). >> >> This is part of a longer code, but I can not get this section done properly. >> I would appreciate any kind of ideas. > It is always difficult to get this sort of problem right using loops. > This is not a complete solution because it does not deal with what to > do when the signal is negative, but this can be added into the > solution easily. > > So, you want the output to be > > - the signal from a zero-crossing until the signal reaches the reference value > - the reference value until the signal reaches a zero-crossing > > Therefore the problem can be reduced to that of generating a signal to > toggle between the two, for example: > > s =sin(linspace(0,2*pi,400)); > r=.8; > toggle = [0,cumsum(max(diff(s>r),0)+min(diff(s>0),0))]; > > # now fenerate the output signal > out=s.*(1-toggle)+r*toggle; > > # and plot input and output > plot(1:400,s,out) > > You will need to deal with handling the negative side of things, deal > with zero-crossing correctly and decide whether I extended toggle to > the correct length correctly for your problem. > > Cheers... Ian > > This whole thing looks like a trivial piece-wise function to me. Detecting zero-crossing shouldn't be a problem since the function is based on 'sin'. so zero crossing occur when argument transition from 0-macheps to 0+macheps and from pi-macheps to pi+macheps. --Sergei.