Function cos

Synopse

Create a cosine wave image.

  • f = iacos(s, t, theta, phi)

    • f: Image.
  • s: Image. size: [rows cols].
  • t: Image. Period: in pixels.
  • theta: Double. spatial direction of the wave, in radians. 0 is a wave on the horizontal direction.
  • phi: Double. Phase

In [2]:
import numpy as np

def cos(s, t, theta, phi):
    r, c = np.indices(s)
    tc = t / np.cos(theta)
    tr = t / np.sin(theta)
    f = np.cos(2*np.pi*(r/tr + c/tc) + phi)
    return f

Description

Generate a cosine wave image of size s with amplitude 1, period T, phase phi and wave direction of theta. The output image is a double array.

Examples


In [1]:
testing = (__name__ == "__main__")
if testing:
    ! jupyter nbconvert --to python cos.ipynb
    import numpy as np
    import sys,os
    ia898path = os.path.abspath('../../')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia


[NbConvertApp] Converting notebook cos.ipynb to python
[NbConvertApp] Writing 2809 bytes to cos.py

Example 1


In [2]:
if testing:

    f = ia.cos([128,256], 100, np.pi/3, 0)
    ia.adshow(ia.normalize(f, [0,255]))


Equation

$$ \begin{matrix} f(r,c) & = & cos( 2\pi (\frac{1}{T_r}r + \frac{1}{T_c}c) + \phi) \\ f(r,c) & = & cos( 2\pi (\frac{v}{H}r + \frac{u}{W}c) + \phi) \\ T_r & = & \frac{T}{sin(\theta)} \\ T_c & = & \frac{T}{cos(\theta)} \\ u & = & \frac{W}{T_c} \\ v & = & \frac{H}{T_r} \end{matrix} $$
  • $\theta$ is the direction of the cosine wave.
  • $T$ is the wave period, in number of pixels.
  • $T_r$ and $T_c$ are the period or wave length in the vertical and horizontal directions, respectively, in number of pixels.
  • $H$ and $W$ are the number of image rows and columns, respectively.
  • $v$ and $u$ are the normalized frequency in the horizontal and vertical directions, respectively, in cycles per image dimension.

See Also

  • iacosdemo iacosdemo -- Illustrate discrete cosine wave and its DFT showing its periodic nature.

In [3]:
if testing:
    print('testing cos')
    print(repr(np.floor(0.5 + 127*(ia.cos([7,10], 3, np.pi/4, 0)+1))) == repr(np.array(
          [[ 254.,  138.,    2.,   93.,  246.,  182.,   18.,   52.,  223.,  219.],
           [ 138.,    2.,   93.,  246.,  182.,   18.,   52.,  223.,  219.,   48.],
           [   2.,   93.,  246.,  182.,   18.,   52.,  223.,  219.,   48.,   21.],
           [  93.,  246.,  182.,   18.,   52.,  223.,  219.,   48.,   21.,  187.],
           [ 246.,  182.,   18.,   52.,  223.,  219.,   48.,   21.,  187.,  244.],
           [ 182.,   18.,   52.,  223.,  219.,   48.,   21.,  187.,  244.,   88.],
           [  18.,   52.,  223.,  219.,   48.,   21.,  187.,  244.,   88.,    3.]])))


testing cos
True

In [ ]: