Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.70 KB | None | 0 0
  1. function out = trianglewave(t,f,shape)
  2.  
  3. %TRIANGLEWAVE generates triangle or sawtooth waves.
  4. % trianglewave = TRIANGLEWAVE(T,FREQ,SHAPE)
  5. %
  6. % TRIANGLEWAVE generates a triangle wave of frequency FREQ for the input
  7. % time vector T. If SHAPE is specified, it can also generate SAWTOOTH
  8. % waves. The waves it generates has peaks of +1 and -1.
  9. %
  10. % TRIANGLEWAVE(T) generates a triangle wave with a default frequency of
  11. % 40 Herz.
  12. % TRIANGLEWAVE(T,FREQ) uses a frequency specified by FREQ. A FREQ value
  13. % less than the Nyquist frequency is recommended.
  14. % TRIANGLEWAVE(T,FREQ,SHAPE) generates a wave with the specific SHAPE.
  15. % SHAPE could be either 'SAWTOOTH0' which is a inverse saw wave, or
  16. % 'SAWTOOTH1' which is the conventional sharply drop saw wave. The
  17. % default SHAPE is 'TRIANGLE'.
  18. %
  19. % The SAWTOOTH function from signal processing toolbox does the same job,
  20. % using a different algorithm. SAWTOOTH is sometimes inaccurate.
  21. %
  22. % Example:
  23. % t = 0:1/200:0.5;
  24. % w = trianglewave(t,30);
  25. % figure; plot(w);
  26. % v = sawtooth(2*pi*30*t,0.5); % comparing with SAWTOOTH:
  27. % figure; plot(v);
  28. %
  29. % See also SQUARE, SAWTOOTH.
  30.  
  31. % Code by Siyi Deng, sdeng@uci.edu
  32. % HNL, COG SCI, UCI.
  33. % 08-16-2006; Ver 1.0;
  34. % 08-17-2006; Ver 1.01; efficency improvement.
  35. % 08-21-2006; Ver 1.02; vector dimension check.
  36.  
  37. if nargin == 0, t = linspace(0,1/40,16000/40); end;
  38. if nargin < 2, f = 40; end;
  39. if nargin < 3, shape = 't'; end;
  40.  
  41. if ~isvector(t), error('ERROR: Time array dimension mismatch.'); end
  42.  
  43. ts = length(t);
  44. fs = ts/(t(end) - t(1)); % sampling frequency;
  45.  
  46. if f>fs, error('ERROR: Input frequency exceeds sampling frequency.'); end
  47.  
  48. ds = fs/f; % samples per duration;
  49.  
  50. acme = floor(1:ds:ts);
  51. intv = acme(2:end) - acme(1:end-1)-1;
  52. intv = [intv intv(end)];
  53. intv(intv<1) = 1;
  54.  
  55. lin = 1:1:ts;
  56. ind = ceil(lin./ds);
  57. ind(ind>length(acme)) = 1;
  58.  
  59. switch shape
  60. case {'s0','S0','sawtooth0','SAWTOOTH0','Sawtooth0'}
  61. out = (lin-acme(ind))./intv(ind); % height normalization by period;
  62. out = out.*2-1;
  63. case {'s1','S1','sawtooth1','SAWTOOTH1','Sawtooth1'}
  64. out = (lin-acme(ind))./intv(ind) ;
  65. out = (1-out).*2-1;
  66. otherwise
  67. apex = floor(ds/2:ds:ts);
  68. out = (lin-apex(ind));
  69.  
  70. % positive part normalization;
  71. pIntv = acme(2:end) - apex(1:end-1)-1;
  72. pIntv = [pIntv pIntv(end)];
  73. pIntv(pIntv<1) = 1;
  74. pInd = ind;
  75. pInd(out<0) = 1;
  76. pos = out;
  77. pos(pos<0) = 0;
  78. pos = pos./pIntv(pInd);
  79.  
  80. % negtive part normalization;
  81. nIntv = apex(1:end-1)-acme(1:end-1)+1;
  82. nIntv = [nIntv nIntv(end)];
  83. nIntv(nIntv<1) = 1;
  84. nInd = ind;
  85. nInd(out>0) = 1;
  86. neg = out;
  87. neg(neg>0) = 0;
  88. neg = neg./nIntv(nInd);
  89.  
  90. % final output normalization;
  91. out = 1-abs(pos + neg);
  92. out = out.*2 -1;
  93. out(out>1) =1;
  94. out(out<-1) = -1;
  95. end
  96.  
  97. if size(t,2) == 1, out = out'; end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement