rcosfltk.m
Upload User: loeagle
Upload Date: 2013-03-02
Package Size: 1236k
Code Size: 7k
Development Platform:

Matlab

  1. %Correction by GB 16.1.98:
  2. %sqrt was not considered !
  3. %Correction done for FIR only
  4. function [y, t] = rcosfltk(x, Fd, Fs, type_flag, R, Delay, tol)
  5. %RCOSFLT Filters the input signal using raised cosine filter.
  6. %       Y = RCOSFLT(X, Fd, Fs) filters the input signal X using raised cosine
  7. %       (R-C) FIR filter. The sample frequency for X is Fd (Hz). The sample
  8. %       frequency for Y is Fs. Fs must be larger than Fd. Fs/Fd must be an
  9. %       integer. The rolloff factor R is a default of .5. The time delay is a
  10. %       default of 3. The extra delay has been taken off from output Y,
  11. %       such that offset Fs/Fd - 1 is the best decision point (such as used
  12. %       in function MODMAP). The row number (or vector length) of Y is Ys/Yd
  13. %       times that of X's.
  14. %
  15. %       Y = RCOSFLT(X, Fd, Fs, TYPE_FLAG) gives specific computation
  16. %       instruction. TYPE_FLAG is a string, which can be one of the following.
  17. %       'fir'    Use FIR R-C filter (default).
  18. %       'iir'    Use IIR R-C filter.
  19. %       'normal' Use normal R-C filter (default), in contrast to 'qart'.
  20. %       'sqrt'   Use square root raised cosine filter.
  21. %       'wdelay' Keep the full length of the filtered result, in which case
  22. %                the row number (or vector length) of Y is 
  23. %                (length_of_X + DELAY)*Fs/Fd. The value DELAY is a default of
  24. %                3. The function default has had the delay cut off from
  25. %                the output.
  26. %       'Fs'     X is input with sample frequency Fs. In this case, only
  27. %                elements X(i*Fs/Fd+1,:) are used in the calculation. All
  28. %                others are discarded.
  29. %       'filter' Means filter is provided in this function call. When 
  30. %                TYPE_FLAG contains 'filter', the calling format is
  31. %                Y = RCOSFLT(X, Fd, Fs, TYPE_FLAG, Delay, NUM).
  32. %                When TYPE_FLAG contains both 'filter' and 'iir', the calling
  33. %                format is Y = RCOSFLT(X, Fd, Fs, TYPE_FLAG, DELAY, NUM, DEN).
  34. %                where NUM and DEN are numerator and denominator of raised
  35. %                cosine filter.  DELAY is the filter time delay is 1/Fd steps.
  36. %                If default DELAY has been used in the raised cosine design,
  37. %                use an empty matrix here. The raised cosine filter can be
  38. %                designed using function RCOSINE.
  39. %       'default' Use all default values.
  40. %       TYPE_FLAG can be a combination of the above string with a '/' as
  41. %       separation. For example, TYPE_FLAG = 'iir/sqrt'.
  42. %
  43. %       Y = RCOSFLT(X, Fd, Fs, TYPE_FLAG, R) gives rolloff factor. In
  44. %       general, it is a real number in range [0, 1].
  45. %
  46. %       Y = RCOSFLT(X, Fd, Fs, TYPE_FLAG, R, DELAY) gives the delay in the
  47. %       measure of 1/Fd time steps. DELAY should be a positive integer.
  48. %       DELAY/Fd will be the time delay in the raised cosine filter design.
  49. %       If the default time delay is used, assign an empty matrix for DELAY.
  50. %
  51. %       Y = RCOSFLT(X, Fd, Fs, TYPE_FLAG, R, DELAY, TOL) provides the
  52. %       tolerance in IIR filter design. The default value is .01.
  53. %
  54. %       [Y, T] = RCOSFLT(...) outputs the time vector.
  55. %
  56. %       See also RCOSINE.
  57. %       Wes Wang 1/19/95, 10/11/95.
  58. %       Copyright (c) 1995-96 by The MathWorks, Inc.
  59. %       $Revision: 1.1 $  $Date: 1996/04/01 18:02:18 $
  60. %default tolerance
  61. if nargin < 7
  62.     tol = .01;
  63. end;
  64. %default delay
  65. if nargin < 6
  66.     Delay = 3;
  67. elseif isempty(Delay)
  68.     Delay = 3;
  69. elseif Delay <= 0
  70.     error('DELAY must be a positive integer in RCOSFLT.')
  71. elseif ceil(Delay) ~= Delay
  72.     error('DELAY in RCOSFLT must be an integer.')
  73. end;
  74. %default rolloff factor
  75. if nargin < 5
  76.     R = .5;
  77. elseif R < 0
  78.     error('The Rolloff factor in RCOSFLT cannot be a negative number.')    
  79. end;
  80. %default type_flag
  81. if nargin < 4
  82.     type_flag = '';
  83. elseif ~isstr(type_flag) & ~isempty(type_flag)
  84.     error('TYPE_FLAG in RCOSFLT must be a string.');
  85. end;
  86. %not enough input varible.
  87. if nargin < 3
  88.     error('Not enough input variable for RCOSFLT.')
  89. end;
  90. %process the inptu variable x
  91. if isempty(x)
  92.     y = [];
  93.     return;
  94. end;
  95. [len_x_o, wid_x_o] = size(x);
  96. if min(len_x_o, wid_x_o) == 1
  97.     x = x(:);
  98. end;
  99. [len_x, wid_x] = size(x);
  100. FsDFd = Fs/Fd;
  101. if ceil(FsDFd) ~= FsDFd
  102.     error('Fs/Fd must be an integer.')
  103. end;
  104. type_flag = lower(type_flag);
  105. %filter type.
  106. if findstr(type_flag, 'sqrt')
  107.     filt_type = 'sqrt';
  108. else
  109.     filt_type = 'normal';
  110. end;
  111. %design the filter.
  112. if findstr(type_flag, 'fir')
  113.     if findstr(type_flag, 'filter')
  114.         if nargin < 5
  115.             error('Not enough input variable, FIR filter has to be assigned.')
  116.         else
  117.             num = R;
  118.         end
  119.     else
  120.         num = rcosfir(R, Delay, FsDFd, 1/Fd, filt_type)
  121.     end;
  122.     den = 1;
  123. else
  124.     if findstr(type_flag, 'filter')
  125.         if nargin < 6
  126.             error('Not enough input variable, IIR filter has to be assigned.')
  127.         else
  128.             num = R;
  129.             den = Delay;
  130.         end
  131.     else
  132.         [num, den] = rcosiir(R, Delay, FsDFd, 1/Fd, tol);
  133.     end;
  134. end;
  135. %make the x to have the sample time Fs
  136. if findstr(type_flag, 'fs')
  137.     xx = zeros(len_x+Delay*FsDFd, wid_x);
  138.     for i = 1 : FsDFd : len_x
  139.         xx(i, :) = x(i, :);
  140.     end;
  141. else
  142.     xx = zeros((len_x+Delay)*FsDFd, wid_x);
  143.     for i = 1 : len_x
  144.         xx((i-1)*FsDFd+1, :) = x(i, :);
  145.     end;
  146. end;
  147. %filtering
  148. for i = 1:wid_x
  149.     xx(:, i) = filter(num, den, xx(:, i));
  150. end;
  151. cut_length_b = (Delay - 1) * FsDFd  + 2;
  152. cut_length_e = size(xx, 1) - (FsDFd - 1);
  153. t = [0:size(xx, 1)]/Fs;
  154. if nargout < 1
  155.     % plot the result in comparing the input digit
  156.     xx = xx(cut_length_b:cut_length_e, :);
  157.     t = t(cut_length_b : cut_length_e);
  158.     if isempty(findstr(type_flag, 'Fs'))
  159.         yy = zeros((len_x)*FsDFd, wid_x);
  160.         for i = 1 : len_x
  161.             if i == 1
  162.                 yy((i-1)*FsDFd+1:i*FsDFd, :) = ones(FsDFd, 1) * x(i, :);
  163.             else
  164.                 yy((i-1)*FsDFd+1:i*FsDFd, :) = x(i*ones(1,FsDFd), :);
  165.             end;
  166.         end;
  167.         x = yy;
  168.         clear yy
  169.     end;
  170.     if (size(x, 2) == 1) | (size(x, 2) > 16)
  171.         plot(t, [xx x])
  172.     else
  173.         col='ymcrgbw';
  174.         plt = [];
  175.         for i = 1 : size(x, 2)
  176.             if i > 1
  177.                 plt = [plt, ',t,[xx(:,', num2str(i), '),x(:,', num2str(i), ')],''', col(rem(i-1,7)+1),''''];
  178.             else
  179.                 plt = 't,[xx(:,1), x(:,1)],''y''';
  180.             end;
  181.         end;
  182.         plt = ['plot(', plt, ')'];
  183.         eval(plt);
  184.         ylabel('Same color for original-filted pair')
  185.     end;
  186.     title('Raised cosine filted signal v.s. input signal.')
  187.     xlabel('Time (sec, original signal shifted)')
  188. elseif findstr(type_flag, 'wdelay')
  189.     y = xx;
  190. else
  191.     y = xx(cut_length_b:cut_length_e, :);
  192.     t = t(cut_length_b : cut_length_e);
  193. end;
  194.     
  195. %--end of rcosflt.m--