给出单个正弦波值和周期递归地创建正弦波 [英] Recursively create a sine wave given a single sine wave value and the period

查看:235
本文介绍了给出单个正弦波值和周期递归地创建正弦波的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Octave写一个.oct函数,给定一个正弦波值,在-1和1之间,以及正弦波周期,返回一个周期长度的正弦波矢量,向量中的最后一个值给定正弦波值。我的代码到目前为止:

  #include< octave / oct.h> 
#include< octave / dColVector.h>
#include< math.h>
#define PI 3.14159265

DEFUN_DLD(sinewave_recreate,args,args(0)sinewave value,args(1)is period)
{
octave_value_list retval ;

double sinewave_value = args(0).double_value();
double period = args(1).double_value();
ColumnVector output_sinewave(period);
double degrees_inc = 360 / period;
double output_sinewave_degrees;

output_sinewave_degrees = asin(sinewave_value)* 180 / PI;
output_sinewave(period-1)= sin(output_sinewave_degrees * PI / 180);

for(octave_idx_type ii(1); ii {
output_sinewave_degrees = output_sinewave_degrees - degrees_inc;

if(output_sinewave_degrees <0)
{
output_sinewave_degrees + = 360;
}

output_sinewave(period-1-ii)= sin(output_sinewave_degrees * PI / 180);
}

retval(0)= output_sinewave;

return retval;
}

通过这个,我的意思是,它有时重现正弦波相当准确,其他时候它的方式关闭。我简单地通过创建给定的正弦波,获取时间的最后一个值,并将其插入到函数中以通过时间向后重建正弦波,然后比较两者的图来确定这一点。显然我做错了事,但我似乎不能确定什么。

解决方案

以一些三角形身份开始:

  sin(x)^ 2 + cos(x)^ 2 == 1 
sin(x + y)== sin(x)* cos(y)+ sin (x)
cos(x + y)== cos(x)* cos(y)-sin(x)* sin(y)

给定一个点 x 的正弦和余弦,我们可以在大小 d ,在预计算 sd = sin(d) cd = cos(d)

  sin(x + d)= sin(x)* cd + cos(x)* sd 
cos(x + d)= cos(x)* cd - sin(x)* sd

给定初始正弦值,可以计算初始余弦值:

  cos(x)= sqrt(1  -  sin注意,有两种可能的解决方案,对应于两个可能的平方根(x,y,z)值。另请注意,这些身份中的所有角度都是以弧度表示,如果您要返回波形, d 需要为负值。


I am trying to write a .oct function for Octave that, given a single sine wave value, between -1 and 1, and sine wave period, returns a sine wave vector of period length with the last value in the vector being the given sine wave value. My code so far is:

#include <octave/oct.h>
#include <octave/dColVector.h>
#include <math.h>
#define PI 3.14159265

DEFUN_DLD (sinewave_recreate, args, , "args(0) sinewave value, args(1) is period")
{
octave_value_list retval;

double sinewave_value = args(0).double_value (); 
double period = args(1).double_value ();  
ColumnVector output_sinewave(period);                
double degrees_inc = 360 / period;
double output_sinewave_degrees;

output_sinewave_degrees = asin( sinewave_value ) * 180 / PI;
output_sinewave(period-1) = sin( output_sinewave_degrees * PI / 180 );

for (octave_idx_type ii (1); ii < period; ii++) // Start the loop
   {
   output_sinewave_degrees = output_sinewave_degrees - degrees_inc;

   if ( output_sinewave_degrees < 0 )
   {
   output_sinewave_degrees += 360 ;
   }  

   output_sinewave( period-1-ii ) = sin( output_sinewave_degrees * PI / 180 );
   }

retval(0) = output_sinewave;                                                          

return retval;                                                                        
}

but is giving patchy results. By this I mean that it sometimes recreates the sine wave quite accurately and other times it is way off. I have determined this simply by creating a given sine wave, taking the last value in time and plugging this into the function to recreate the sine wave backwards through time and then comparing plots of the two. Obviously I am doing something wrong, but I can't seem to identify what.

解决方案

Lets start with some trigonometric identities:

sin(x)^2 + cos(x)^2 == 1
sin(x+y) == sin(x)*cos(y) + sin(y)*cos(x)
cos(x+y) == cos(x)*cos(y) - sin(x)*sin(y)

Given the sine and cosine at a point x, we can exactly calculate the values after a step of size d, after precalculating sd = sin(d) and cd = cos(d):

sin(x+d) = sin(x)*cd + cos(x)*sd
cos(x+d) = cos(x)*cd - sin(x)*sd

Given the initial sine value, you can calculate the initial cosine value:

cos(x) = sqrt(1 - sin(x)^2)

Note that there are two possible solutions, corresponding to the two possible square-root values. Also note that all the angles in these identities are in radians, and d needs to be negative if you're going back through the wave.

这篇关于给出单个正弦波值和周期递归地创建正弦波的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆