# Sine wave code Classic List Threaded 15 messages Open this post in threaded view
|

## Sine wave code

 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. Thanks.   -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: Sine wave code

 On Tue, Dec 29, 2020 at 9:03 AM Blaz <[hidden email]> 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. Thanks.  -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html Can you show us your attempt?-- DAS Open this post in threaded view
|

## Re: Sine wave code

 Sure u(k) and ipp(k) are the values that need to be cut if u(k) is above the Cutoff value. This code cuts the sine wave but only until the sine value is higher than the Cutoff value. I would like to prolong this until the next zero crossing, but Im missing something. ------------------------------------------------------------------------ for k=1:1:n                                                     uac(k)=(ip(k)/CT)*(Rct+Rb);   u(k)=uac(k)*kdc*krem;   if (k > 1) && (ip(k)*ip(k-1) < 0)     %zero crossings                   zx = 1;                                                 else    zx = 0;                                               end   if abs(u(k)) <  Cutoff                                   ipp(k) = ip(k);                                       elseif u(k) > 0     ipp(k) = Ippmax;   elseif u(k) < 0     ipp(k) = -Ippmax;   end     if u(k) > 0                                          % cutoff Value check     u(k) = Cutoff;   end   if u(k) < 0     u(k) = -Cutoff;   end       if ipp(k) == 0                                      % zero crossing     j = 0;   end   if zx > 0     j = 1;   end     integral(k)=j;   CTsat(k) = abs(sign(ipp(k)));%*integral(k));         % izracunamo obmocje nasicenja   end -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: Sine wave code

 In reply to this post by Blaz On 29/12/2020 11:04, Blaz 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. > > Thanks. > > > > > > -- > Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html> > Have you ever heard of 'asin', 'acos' functions ? https://en.wikipedia.org/wiki/Inverse_trigonometric_functions would be a good start. --Sergei.
Open this post in threaded view
|

## Re: Sine wave code

 In reply to this post by Blaz Am 29.12.20 um 16:32 schrieb Blaz: > Sure > > u(k) and ipp(k) are the values that need to be cut if u(k) is above the > Cutoff value. This code cuts the sine wave but only until the sine value is > higher than the Cutoff value. I would like to prolong this until the next > zero crossing, but Im missing something. > > ------------------------------------------------------------------------ > for k=1:1:n > >    uac(k)=(ip(k)/CT)*(Rct+Rb); >    u(k)=uac(k)*kdc*krem; >    if (k > 1) && (ip(k)*ip(k-1) < 0)     %zero crossings >      zx = 1; >    else >     zx = 0; >    end > >    if abs(u(k)) <  Cutoff >      ipp(k) = ip(k); >    elseif u(k) > 0 >      ipp(k) = Ippmax; >    elseif u(k) < 0 >      ipp(k) = -Ippmax; >    end >     >    if u(k) > 0                                          % cutoff Value check >      u(k) = Cutoff; >    end >    if u(k) < 0 >      u(k) = -Cutoff; >    end >       >    if ipp(k) == 0                                      % zero crossing >      j = 0; >    end >    if zx > 0 >      j = 1; >    end >     >    integral(k)=j; >    CTsat(k) = abs(sign(ipp(k)));%*integral(k));         % izracunamo obmocje > nasicenja >     > end This code cannot be run because ip, CT, Rct, RB and so on are missing. Try to create a minimal and reproducible example. -- Andy
Open this post in threaded view
|

## Re: Sine wave code

 As mentioned, it is an excerpt from a wider code. I have enclosed the .m file. If you want to run it you have to input the constants manualy. CTsatreb500.m   -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: Sine wave code

 In reply to this post by Octave - General mailing list Will try...although I will probably not work, because I can have zero crossings which are not periodica (an exp offset in one direction). Still, the problem remains. -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: Sine wave code

 In reply to this post by Blaz On Tue, 29 Dec 2020 at 14:03, Blaz <[hidden email]> 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
Open this post in threaded view
|

## Re: Sine wave code

 On Tue, Dec 29, 2020 at 12:37 PM Ian McCallion <[hidden email]> wrote:On Tue, 29 Dec 2020 at 14:03, Blaz <[hidden email]> 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 How is this?n_points = 200;x = linspace(-5, 5, n_points);y_original = sin(x);threshold = 0.5;% Truncate the sine wavey_truncated = y_original.*(abs(y_original)=threshold).*sign(y_original);y_final = y_truncated;for k=2:n_pointsif y_final(k-1)==threshold && y_final(k-1)*y_final(k)>0y_final(k) = threshold;endendclfplot(x, y_final)It produces the desired result for the test sine wave I put in.
Open this post in threaded view
|

## Re: Sine wave code

 In reply to this post by Ian McCallion On 29/12/2020 19:36, Ian McCallion wrote: > On Tue, 29 Dec 2020 at 14:03, Blaz <[hidden email]> 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.
Open this post in threaded view
|

## Re: Sine wave code

 In reply to this post by BGreen TNX. It works perfectly. -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html
Open this post in threaded view
|

## Re: Sine wave code

 On Tue, Dec 29, 2020 at 2:53 PM Blaz <[hidden email]> wrote:TNX. It works perfectly. -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html You're welcome. Please let me know if you need any of it to be explained.
Open this post in threaded view
|

## Re: Sine wave code

 OK. Thanks again. -- Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html