How does one generate an image of a sine wave with (h,k) indices (2,2)?

568 Views Asked by At

I am trying to generate an image of a gradient that oscillates $h$ times along the $x$-axis and $k$ times along the $y$-axis.

As an example, this image I generated below oscillates $5$ times along the $x$-axis, and $0$ times along the $y$-axis:

$f(x) = \sin(5x + 0)$

The image below on the left is the sum of two waves: one wave $(h, k) = (4, 4)$, and the other with $(h, k) = (-20, 19)$

enter image description here

The images I want to generate are just an array of 100x100 pixels. I can't seem to figure out how to make the wave travel diagonally. The diagonal waves cut each respective axis into a number of pieces -- this is what defines the h and k index.

To generate the one on the right, I simply made an array that is $100$ values and applied the function above on those values.

Apologies if this is the wrong forum to post on! Thank you in advance

2

There are 2 best solutions below

3
On BEST ANSWER

You know that $f(x)=\sin(x)$ completes one period on $[0,2\pi]$.

Therefore $f(x)=\sin(2\pi x)$ completes one period on $[0,1]$.

Then $f(x)=\sin(2\pi hx)$ completes $h$ periods on the interval $[0,1]$.

In the plane use $f(x,y)=\sin(2\pi hx+2\pi yk)$.

On interval $[0,100]\times[0,100]$ it is $f(x,y)=\sin(200\pi hx+200\pi yk)$.

0
On

I have accepted M. Nestor's answer -- here is the final result

enter image description here

import numpy as np
from random import seed
from random import random
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt

#Globals
resolution = 1000

#Get a random HK plane
def rdmHK():
    return(round(random()*15))

#Get a random amplitude
def rdmAmp():
    return(random()*100)

#Wave generator
def waveGenerator(h, k, a, phi, res):
    mesh = np.fromfunction(lambda x, y: a*np.sin(2*np.pi*h*x/res + 2*np.pi*k*y/res + phi), shape = (res,res))
    return mesh

#Make a blank image, 1000x1000
img = np.zeros((resolution, resolution))

#Add together 100 random waves of various resolutions
for x in range(15):
    img = np.add(img, waveGenerator(rdmHK(), rdmHK(), rdmAmp(), 0, resolution))
    print('added wave ' + str(x))

wave = waveGenerator(1,1,1,0,resolution)

plt.imshow(img)
plt.show()