Sine Wave Compress/Expand per 180

In summary, the conversation discusses the modification of a sine wave using MS-Paint and code. The individual is seeking advice on how to compress and expand different parts of the wave smoothly using specific time intervals and a formula based on circle division and phase in radians. They are requesting further assistance in altering their math to achieve the desired result.
  • #1
1plus1is10
51
0
Hello,
I hope you can help me.
I want to "alter" a sine wave.
I would say "modulate", except that I was unsatisfied with the FM formula.

Here's what I want, visualize this:
1) copy-n-paste an image of a sine wave into MS-Paint
2) select part of the image from wave top1-to-bottom1
3) grab the right edge of the selection and shrink it to the left compressing the wave
4) select the next part of the wave bottom1-to-top2
5) grab the left edge of the selection and expand it to the left to fill-in the gap

Here is the sine wave code I've been toying with:
Code:
ttlH=400; ttlW=600;
ttlTime=1000;
xW=ttlW/ttlTime;// width between x1,x2
amp=ttlH/3;// amplitude
center=ttlH/2;// center of sine wave
freq=ttlTime/3;// frequency
for(time=0; time<ttlTime; time++){
	x2 = round(time*xW);
	cirdiv = ((time%freq) / freq);
	phase = cirdiv * 2 * PI;// radians
	y2 = round((sin(phase)*amp) + center);
	if(time>0) DrawLine(x1,ttlH-y1, x2,ttlH-y2);
	x1=x2; y1=y2;
}

I've messed with freq, cirdiv, and phase to no avail.
I would have thought it would be as easy as:

if(cirdiv>=.25 && cirdiv<.75){ z=?; }else{ z=?; }
sin(phase + z)

Nope.
So, I need a mathmetician.
One thing though, I do not want to touch x1,x2.
Thanks
 
Mathematics news on Phys.org
  • #2
I'm not certain enough of the language you are using to write code for you.

Can you translate this into your language?

Suppose your sine wave before modification is sin(f*t) for 0<=t<=infinity.
Suppose you want to compress from t1<=t<=t2 into t1<=t<=t2x.
Suppose you want to expand from t2<=t<=t3 into t2x<=t<=t3.
So you are "dragging" a single point in time from t2 to t2x and having fixed amounts on each side be modified.
Then you have 4 segments of your sine wave:
sin(f*t) for 0<=t<=t1
sin((f*(t*(t1 - t2) + t1*(t2 - t2x)))/(t1 - t2x)), for t1<=t<=t2x
sin((f*(t*(t2 - t3) - t3*(t2 - t2x)))/(t2x - t3)), for t2x<=t<=t3
sin(f*t) for t3<=t<=infinity

That should work, but you must keep 0<=t1<=t2<=t3<=infinity and t1<=t2x<=t3, no letting "time run backwards", but I believe you can drag t2 towards t1, to compress, or towards t3, to expand, the first part and the second part will then compensate.

Carefully check that by plotting it for a variety of t1,t2,t2x,t3 and verify it is correct.
 
Last edited:
  • #3
Bill,
Thanks for your help.

First...
My code laugauge can be anything that draws an image - C,C++,PHP,Java,Javascript.
The DrawLine function would have language specific graphics code.
ttlW,ttlH are image width,height in pixels
x1,x2, y1,y2 are also in pixels

Second...
It looks to me like you're calculating a percentile of time.
I do believe this is on the right path.
But, I'm not sure how I would calculate t1,t2,t2x,t3.
I assume the answer is in the freq?

Instead...
Maybe I should paint you a more accurate mental picture.
Imagine:
1) a continuous sine wave with top-bottom-top-bottom ratio of 50%-50%-50%-50%
2) there is a rope tied across the tops
3) a second rope tied across the bottoms
4) you pull the second rope to the left
5) the top-bottom-top-bottom ratio becomes 40%-60%-40%-60%

I hope this will make the calcs simpler rather than being one wave specfic.
Sorry for the confusion.
 
  • #4
I don't know any of the hundreds of incompatible programming languages that have been introduced in the last twenty years and you probably couldn't use any of the languages that I once knew so I can't write your code for you.

I tried to make it clear that t was time and t1, t2, t2x and t3 were specific times, t1 and t2 are the endpoints of the portion you want to compress, t2 and t3 and the endpoints of the portion you want to expand, t1 and t2x are the endpoints after compression, t2x and t3 are the endpoints after expansion.

I realize that all the things in your head are abundantly clear to you, but I'm not clear enough about what your rope work is doing. I had hoped that you could translate tested algebra into what you needed.

Lets try an even more concrete example and see if we share any common language of communcation.

f=2hz=2*2Pi=2*360 degrees, depending on your personal preference.
Your clock starts at zero seconds and begins counting up.
The first attached picture shows your waveform before modification.
Now, if I understand correctly, you want to
compress from t=1/3 to t=5/4 seconds down to t=1/3 to t=2/3 seconds
and you then want to
expand from t=5/4 seconds to t=7/4 seconds to fill from t=2/3 to t=7/4 seconds.
The second attached picture shows your waveform after modification.

To get those I used f = 2*360 Degrees; t1 = 1/3; t2 = 5/4; t2x = 2/3; t3 = 7/4
in the formulas that I showed you above and plotted the curve.

I presume you can translate time to pixels.

Is any of this helpful or understandable. If not then I'm sorry and I'll let someone else take over and see if they share a common language, that isn't mathematics, with you.
 

Attachments

  • 0.jpg
    0.jpg
    7.5 KB · Views: 378
  • 1.jpg
    1.jpg
    7 KB · Views: 382
  • #5
Yeah, that's what I was thinking you meant, but I'm looking for a slighly different compress/expand.
I'm needing smoothness from one timeframe transition (modification) to the next.
This time I too created a pic for you (by hand).
I hope you can see from my pic how smoothly the curve adjusts.
Thanks again.
 

Attachments

  • 2011-05-24_163636.gif
    2011-05-24_163636.gif
    2.6 KB · Views: 455
  • #6
I wrote:
Suppose you want to compress from t1<=t<=t2 into t1<=t<=t2x.
Suppose you want to expand from t2<=t<=t3 into t2x<=t<=t3.

So choose t1, t2, t2x and t3 to compress where you want and expand where you want.

For example
f = 2Hz; t1 = 1/8; t2 = 3/8; t2x = 5/16; t3 = 5/8.
 

Attachments

  • 3.jpg
    3.jpg
    7.6 KB · Views: 373
Last edited:
  • #7
Perfect. Your modified wave looks exactly like what I want.
But I'm stumped.

My sine wave formula uses circle division:
0=0, 90=.25, 180=.5, 270=.75, 360=1
On Wikipedia it's called "Turns": http://en.wikipedia.org/wiki/Turn_(geometry )

I use this to calculate phase measured in radians:
0=0, 90=.5Pi, 180=Pi, 270=1.5Pi, 360=2Pi
http://en.wikipedia.org/wiki/Radian

I have seen similar formulas to what you did.
An example would be the phase shift here:
http://en.wikipedia.org/wiki/Phase_(waves )

I have also found a conversion from hertz to radians:
http://en.wikipedia.org/wiki/Hertz

So like you said this is about algebra.
But I am clueless as to how to alter my math.
My phase must be in radians not hertz.

I hope you can help me further.
(fingers crossed)
 
Last edited by a moderator:
  • #8
Okay, I'm giving it a try.
For your t=1 I need my ttlTime=360
That way for your f=2Hz my freq=ttlTime/2=180
So for you, a 1 Hz sine wave is complete at t=0.5
And now for me, I also get 0.5 with 180/360

I'll keep going down this path until I crack it.
Wish me luck.
 
  • #9
I can draw a standard sine wave with my cirdiv step.
But, I get hung up on "f=2hz=2*2Pi=2*360" plugged in sin(f*t);
I've tried: f=180*2*PI; t=i/360; phase=f*t; (skip the cirdiv calc)
I've tried other variations. Nothing works.
It's not even drawing the standard sine wave trying it this way.

So... I need a mathematician like I originally said.
I just cannot make the conversion.

Help.
 
  • #10
Finally. I got it.

To mimic Bill, I needed freq=2;
Then I check my time i and do Bill's formula:
Code:
if(i>=45 && i<113){
	phase = (f * ((t*-.25)+.0078125))/-.1875;
}else if(i>=113 && i<225){
	phase = (f * ((t*-.25)-.0390625))/-.3125;
}
As you can see, I had to account for the 1/16 shift in time from 135 to 113

I'll be able to make it work now.
Thanks Bill.
 
  • #11
It turns out that I was premature in my excitement.

How can I apply the formula to every wave?
sin((f*(t*(t1 - t2) + t1*(t2 - t2x)))/(t1 - t2x)), for t1<=t<=t2x
sin((f*(t*(t2 - t3) - t3*(t2 - t2x)))/(t2x - t3)), for t2x<=t<=t3

I assume I need to add a multiple to t1,t2,t2x,t3.
 
  • #12
Okay, this time I'm finally finished. Seriously.
So, in case anyone else comes across this from Google...

I replaced freq with freqW.

hertz=2;
f=hertz*2*PI;
freqW=ttlTime/hertz;
cirdiv=(i%floor(freqW)) / freqW;
t=cirdiv/hertz;

Then I adjust with a 0.5 shift:
phase = (f *( (t*t1_2) + (t1*t2_2x) +0.5 ))/(t1 - t2x);// t1<=t<=t2x
phase = (f *( (t*t1_2) - (t3*t2_2x) -0.5 ))/(t2x - t3);// t2x<=t<=t3

Thanks again Bill.
 

Related to Sine Wave Compress/Expand per 180

1. What is a sine wave compress/expand per 180?

A sine wave compress/expand per 180 is a mathematical operation that changes the amplitude and period of a sine wave by compressing or expanding it by a factor of 180.

2. How is a sine wave compress/expand per 180 used in science?

Sine wave compress/expand per 180 is commonly used in signal processing and audio engineering to alter the frequency and pitch of sound waves. It is also used in various scientific experiments and simulations.

3. What is the formula for sine wave compress/expand per 180?

The formula for sine wave compress/expand per 180 is y = A*sin(180x), where A is the amplitude and x is the input value.

4. How does sine wave compress/expand per 180 differ from regular sine waves?

A regular sine wave has an amplitude of 1 and a period of 360 degrees, while a sine wave compress/expand per 180 has an amplitude and period that can vary depending on the compression or expansion factor applied.

5. Can sine wave compress/expand per 180 be reversed?

Yes, sine wave compress/expand per 180 can be reversed by using the inverse operation, which is sine wave compress/expand per -180. This will restore the original amplitude and period of the sine wave.

Similar threads

  • Advanced Physics Homework Help
Replies
1
Views
3K
  • Introductory Physics Homework Help
Replies
5
Views
2K
Back
Top