#!/usr/bin/env python # coding: utf-8 # ## MAT 201A Winter 2016 # ### HW2 # Junxiang Yao # In[104]: get_ipython().run_line_magic('pylab', 'inline') from __future__ import print_function from __future__ import division # In[105]: img = imread('M.jpg') imshow(img) # In[106]: img.shape # #### I get the width and length of the image, and use those number to creat an two dimentional array below. # In[107]: newimg = zeros([1024,1024],uint8) # #### I put the mean of the r,g,b value from the pixel on the image into the newimg array to create an acromatic image which will enable ifft with ease later. # In[108]: height = 1024 width = 1024 for j in range(0,width): for i in range(0,height): newimg [i,j] = img[i,j,0]/3 + img[i,j,1]/3 + img[i,j,2]/3 imshow(newimg,cmap=cm.gray) colorbar() print # #### I regarded the image as both magnitude spectrum and phase spectrum. # In[175]: mag_spec = zeros([width*height],uint8) phs_spec = zeros([width*height],uint8) for j in range(0,width): for i in range(0,height): mag_spec[j+i*width] = newimg [i,j] phs_spec[j+i*width] = newimg [i,j]/255.0*pi-pi/2 # #### I make two one dimentional arrays to save the value from the newimg array. The mag_spec array is using the value directly as the magnitude value. The phs_spec array changes range of the value from the new img array into -pi/2~pi/2, and uses this value as the phase value. # In[176]: X = [np.complex(cos(phs)* mag, -sin(phs)* mag) for mag, phs in zip(mag_spec, phs_spec)] index = range(0, len(X), 1024) sig = [] for i in range(len(index)-1): tempt = (X[index[i]:index[i+1]]) x = fft.ifft(tempt) * 8 sig.append(x) sig=array(sig).flatten() print(array(sig).shape) # #### I used the elements from the phs_spec array and the the mag_spec array to create a complex array. And I used 1024, which is the original width of the picture as the window width value, and I did ifft to each lines from the original image since i supposed to regard this picture as a spectrum. # In[177]: sigout = list((x.astype(int16)) * 150) plot(sigout) title('output signal') xlabel('time') ylabel('magnitude') # In[178]: from IPython.display import Audio Audio(sigout,rate = 3000) # #### I think the result is kind of weird and short, and I think that may caused by the phase spectrum I used. So I tried another way. # In[179]: a = [] # #### This time I assumed that all the phase are 0, so I used the whole picture as magnitude spectrum and use irfft on each rows directly. # In[180]: for i in range(0,height): a.append(fft.irfft(newimg [i,:])) # In[181]: sig = array(a).flatten() # In[182]: sigout = list((sig.astype(int16)) * 200) plot(sigout) title('output signal') xlabel('time') ylabel('magnitude') # In[183]: sigout = list((sig.astype(int16)-200) * 50) plot(sigout) title('output signal') xlabel('time') ylabel('magnitude') # In[184]: from IPython.display import Audio Audio(sigout,rate = 176400)