#! /usr/bin/env python # def sinus ( m ): #*****************************************************************************80 # ## SINUS samples a noisy sine function. # # Discussion: # # For any given value of M, the same set of X and S will always be returned. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 February 2019 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the number of values desired. # # Output, real X(M), S(M), abscissas and values for the noisy sine function. # import numpy as np # # R1 and R2 are the magnitudes of the two noise components. # r1 = 0.1 r2 = 0.1 # # Initialize the random number generator so that it always returns the same # set of values for a given value of M. # seed = m + 123456789 np.random.seed ( seed ) # # Choose M values X in [0,2pi]. # x = 2.0 * np.pi * np.random.rand ( m ) # # Evaluate S = sin(x) + scaled normal noise + uniform noise. # s = np.sin ( x ) \ + r1 * np.random.randn ( m ) * abs ( np.sin ( x ) ) \ + r2 * ( 2.0 * np.random.rand ( m ) - 1.0 ) # # For convenience in plotting, sort by X value. # ind = np.argsort ( x ) x = x[ind] s = s[ind] data = np.zeros ( [ m, 2 ] ) data[0:m,0] = x data[0:m,1] = s return data def sinus_test ( ): #*****************************************************************************80 # ## sinus_test tests sinus. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 20 February 2019 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'sinus_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' sinus() evaluates a noisy sine function.' ) for m in [ 9, 10, 9 ]: data = sinus ( m ) print ( '' ) print ( ' Input m = %d' % ( m ) ) print ( '' ) for i in range ( 0, m ): print ( ' %2d %f %f' % ( i, data[i,0], data[i,1] ) ) # # Terminate. # print ( '' ) print ( 'sinus_test' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): sinus_test ( )