mkSquare.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. % IM = mkSquare(SIZE, PERIOD, DIRECTION, AMPLITUDE, PHASE, ORIGIN, TWIDTH)
  2. % or
  3. % IM = mkSine(SIZE, FREQ, AMPLITUDE, PHASE, ORIGIN, TWIDTH)
  4. %
  5. % Compute a matrix of dimension SIZE (a [Y X] 2-vector, or a scalar)
  6. % containing samples of a 2D square wave, with given PERIOD (in
  7. % pixels), DIRECTION (radians, CW from X-axis, default = 0), AMPLITUDE
  8. % (default = 1), and PHASE (radians, relative to ORIGIN, default = 0).
  9. % ORIGIN defaults to the center of the image. TWIDTH specifies width
  10. % of raised-cosine edges on the bars of the grating (default =
  11. % min(2,period/3)).
  12. %
  13. % In the second form, FREQ is a 2-vector of frequencies (radians/pixel).
  14. % Eero Simoncelli, 6/96.
  15. % TODO: Add duty cycle.
  16. function [res] = mkSquare(sz, per_freq, dir_amp, amp_phase, phase_orig, orig_twidth, twidth)
  17. %------------------------------------------------------------
  18. %% OPTIONAL ARGS:
  19. if (prod(size(per_freq)) == 2)
  20. frequency = norm(per_freq);
  21. direction = atan2(per_freq(1),per_freq(2));
  22. if (exist('dir_amp') == 1)
  23. amplitude = dir_amp;
  24. else
  25. amplitude = 1;
  26. end
  27. if (exist('amp_phase') == 1)
  28. phase = amp_phase;
  29. else
  30. phase = 0;
  31. end
  32. if (exist('phase_orig') == 1)
  33. origin = phase_orig;
  34. end
  35. if (exist('orig_twidth') == 1)
  36. transition = orig_twidth;
  37. else
  38. transition = min(2,2*pi/(3*frequency));
  39. end
  40. if (exist('twidth') == 1)
  41. error('Too many arguments for (second form) of mkSine');
  42. end
  43. else
  44. frequency = 2*pi/per_freq;
  45. if (exist('dir_amp') == 1)
  46. direction = dir_amp;
  47. else
  48. direction = 0;
  49. end
  50. if (exist('amp_phase') == 1)
  51. amplitude = amp_phase;
  52. else
  53. amplitude = 1;
  54. end
  55. if (exist('phase_orig') == 1)
  56. phase = phase_orig;
  57. else
  58. phase = 0;
  59. end
  60. if (exist('orig_twidth') == 1)
  61. origin = orig_twidth;
  62. end
  63. if (exist('twidth') == 1)
  64. transition = twidth;
  65. else
  66. transition = min(2,2*pi/(3*frequency));
  67. end
  68. end
  69. %------------------------------------------------------------
  70. if (exist('origin') == 1)
  71. res = mkRamp(sz, direction, frequency, phase, origin) - pi/2;
  72. else
  73. res = mkRamp(sz, direction, frequency, phase) - pi/2;
  74. end
  75. [Xtbl,Ytbl] = rcosFn(transition*frequency,pi/2,[-amplitude amplitude]);
  76. res = pointOp(abs(mod(res+pi, 2*pi)-pi),Ytbl,Xtbl(1),Xtbl(2)-Xtbl(1),0);
  77. % OLD threshold version:
  78. %res = amplitude * (mod(res,2*pi) < pi);