Sine Wave Compress/Expand per 180

Click For Summary

Discussion Overview

The discussion revolves around modifying a sine wave through compression and expansion techniques. Participants explore mathematical approaches to achieve a visual representation of these alterations, focusing on the manipulation of specific segments of the wave over defined time intervals.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes a method to compress and expand a sine wave by altering specific time intervals, suggesting a mathematical representation using segments of the wave.
  • Another participant expresses uncertainty about how to calculate the specific time points (t1, t2, t2x, t3) needed for the modifications, indicating a reliance on frequency.
  • Some participants propose visualizing the sine wave as a rope being pulled, which affects the wave's top-bottom ratio, suggesting a more intuitive understanding of the compression and expansion process.
  • A participant shares their attempts to translate the mathematical concepts into code, highlighting challenges with phase calculations and the need for clarity in the formulas used.
  • There is a discussion about the relationship between frequency, radians, and the sine wave formula, with participants attempting to reconcile their understanding of these concepts with practical coding implementations.
  • One participant reports success in modifying their sine wave but later questions how to apply the derived formulas universally across different waves.
  • Another participant shares a final solution involving adjustments to the frequency and phase calculations, indicating a resolution to their earlier challenges.

Areas of Agreement / Disagreement

Participants express a range of views on the best methods to achieve the desired sine wave modifications, with no clear consensus reached on a single approach. Several participants agree on the need for precise calculations and visualizations, but differing interpretations of the mathematical relationships remain.

Contextual Notes

Participants acknowledge limitations in their understanding of programming languages and mathematical concepts, which may affect their ability to communicate effectively. There are also unresolved aspects regarding the application of formulas to various waveforms.

Who May Find This Useful

This discussion may be useful for individuals interested in signal processing, computer graphics, or mathematical modeling of waveforms, particularly those looking to manipulate sine waves through programming or mathematical techniques.

1plus1is10
Messages
51
Reaction score
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
 
Physics news on Phys.org
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:
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.
 
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: 465
  • 1.jpg
    1.jpg
    7 KB · Views: 446
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: 527
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: 435
Last edited:
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:
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.
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K