声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2880|回复: 8

[编程技巧] 问个函数返回值的问题

[复制链接]
发表于 2010-11-11 19:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
  1. function [genpoly,t] = rsgenpoly(N, K, varargin)
  2. %RSGENPOLY  Generator polynomial of Reed-Solomon code.
  3. %   GENPOLY = RSGENPOLY(N,K) returns the narrow-sense generator polynomial of a
  4. %   Reed-Solomon code with codeword length N and message length K.  The codeword
  5. %   length N must have the form 2^m-1 for some integer m between 3 and 16.  The
  6. %   output GENPOLY is a Galois row vector that represents the coefficients of
  7. %   the generator polynomial in order of descending powers.  The narrow-sense
  8. %   generator polynomial is (X-alpha)*(X-alpha^2)*...*(X-alpha^(N-K)), where
  9. %   alpha is a root of the default primitive polynomial for the field GF(N+1).
  10. %
  11. %   GENPOLY = RSGENPOLY(N,K,PRIM_POLY) is the same as the syntax above, except
  12. %   that PRIM_POLY specifies the primitive polynomial for GF(N+1) that has alpha
  13. %   as a root.  PRIM_POLY is an integer whose binary representation indicates
  14. %   the coefficients of the primitive polynomial in order of descending powers.
  15. %   To use the default primitive polynomial, set PRIM_POLY to [].
  16. %
  17. %   GENPOLY = RSGENPOLY(N,K,PRIM_POLY,B) returns the generator polynomial
  18. %   (X-alpha^B)*(X-alpha^(B+1))*...*(X-alpha^(B+N-K-1)), where B is an integer
  19. %   and alpha is a root of PRIM_POLY.
  20. %
  21. %   [GENPOLY,T] = RSGENPOLY(...) returns T, the error-correction capability of
  22. %   the code.
  23. %
  24. %   Examples:
  25. %      g  = rsgenpoly(7,3)       % Narrow-sense generator polynomial
  26. %      g2 = rsgenpoly(7,3,13)    % Narrow-sense generator polynomial,
  27. %                                %   with respect to primitive polynomial
  28. %                                %   D^3+D^2+1
  29. %      g3 = rsgenpoly(7,3,[],4)  % Use b=4
  30. %
  31. %   See also GF, RSENC, RSDEC.



  32. %    Copyright 1996-2007 The MathWorks, Inc.
  33. %    $Revision: 1.4.4.2 $  $Date: 2007/09/14 15:57:43 $


  34. % Initial checks
  35. error(nargchk(2,4,nargin,'struct'));

  36. % Number of optional input arguments
  37. nvarargin = nargin - 2;

  38. % Error-correcting capability
  39. t = floor((N-K)/2);
  40. t2 = N-K;
  41. m = log2(N+1);
  42. def_primpoly = 1;
  43. b = 1;  % Default : narrow-sense

  44. if any([~isscalar(N) | ~isscalar(K) | floor(N)~=N | floor(K)~=K])
  45.     error('comm:rsgenpoly:InvalidNK','N and K must be positive scalar integers.');
  46. end

  47. if t2<1
  48.     error('comm:rsgenpoly:NLessThanK','N must be larger than K.');
  49. end

  50. if m~=floor(m) | m<3 | m>16
  51.   error('comm:rsgenpoly:InvalidN','N must equal 2^m-1 for some integer m between 3 and 16.')
  52. end

  53. if ~isempty(varargin)

  54.     prim_poly = varargin{1};

  55.     % Check prim_poly
  56.     if isempty(prim_poly)
  57.         if ~isnumeric(prim_poly)
  58.             error('comm:rsgenpoly:InvalidDefaultPrim_Poly','To use the default PRIM_POLY, it must be marked by [].');
  59.         end
  60.     else
  61.         if ~isscalar(prim_poly)
  62.             error('comm:rsgenpoly:Prim_PolyNotScalar','PRIM_POLY must be a scalar integer.');
  63.         end
  64.         % ZZZ add isprimitive once it's available
  65.         def_primpoly = 0;
  66.     end

  67.     if nvarargin==2
  68.         b = varargin{2};
  69.         if ~isscalar(b) | floor(b)~=b
  70.             error('comm:rsgenpoly:BNotAnInt','B must be an integer scalar.');
  71.         end
  72.     end
  73. end

  74. % Alpha is the primitive element of this GF(2^m) field
  75. if def_primpoly == 1
  76.     alpha = gf(2,m);
  77. else
  78.     alpha = gf(2,m,prim_poly);
  79. end


  80. genpoly = 1;
  81. for k=mod(b+[0:t2-1],N)
  82.     evalc('genpoly = conv(genpoly,[1 alpha.^(k)]);');
  83. end

复制代码


大家帮忙看看
我想获得这个返回值,即将他赋给一个变量POLY
rsgenpoly(31,25)
POLY = [1          17          26          30          27          30          24];

试了改了一下没有成功
多谢啊
回复
分享到:

使用道具 举报

 楼主| 发表于 2010-11-11 23:22 | 显示全部楼层
>> rsgenpoly(31,25)

ans = GF(2^5) array. Primitive polynomial = D^5+D^2+1 (37 decimal)

Array elements =

           1          17          26          30          27          30          24

默认返回这个
 楼主| 发表于 2010-11-11 23:26 | 显示全部楼层
  1. >> POLY=rsgenpoly(31,25);
  2. >> POLY

  3. POLY = GF(2^5) array. Primitive polynomial = D^5+D^2+1 (37 decimal)

  4. Array elements =

  5.            1          17          26          30          27          30          24

  6. >> POLY(1)

  7. ans = GF(2^5) array. Primitive polynomial = D^5+D^2+1 (37 decimal)

  8. Array elements =

  9.            1

  10. >> POLY(1)+POLY(2)

  11. ans = GF(2^5) array. Primitive polynomial = D^5+D^2+1 (37 decimal)

  12. Array elements =

  13.           16
复制代码
怎么把POLY变成普通的向量呢?
发表于 2010-11-12 00:59 | 显示全部楼层
专业不懂, 稍微搜下help, 没试成
建议考量简化下
 楼主| 发表于 2010-11-12 09:23 | 显示全部楼层
回复 4 # Happy99 的帖子

不是太懂,所以说的也不是太明白哈,我在研究一下
就是把poly变量变成普通的变量,你看那个加法就知道了
发表于 2010-11-12 09:46 | 显示全部楼层
只需要在函数的末尾加上
  1. genpoly=genpoly.x;
复制代码
因为gf是个结构体,可以用
  1. open gf
复制代码
查看代码
运行的结果:
  1. >> pp=rsgenpoly(31,25)

  2. pp =

  3.            1          17          26          30          27          30          24
复制代码

评分

1

查看全部评分

 楼主| 发表于 2010-11-12 09:57 | 显示全部楼层
回复 6 # qibbxxt 的帖子

多谢哈,成功了
发表于 2010-11-12 11:13 | 显示全部楼层
建议不要修改源程序,直接使用POLY=rsgenpoly(31,25); pp=POLY.x即可

昨晚第一时间亦是朝这个方向! 但没试对fieldname, 又一时没找到查询函数(应用fieldnames, 原少个s字)
原来那些值是'x'!
请教gf class的cmd win显示好像与结构体不同, 结构体直接就有fieldname显示!?

评分

1

查看全部评分

 楼主| 发表于 2010-11-12 15:47 | 显示全部楼层
回复 8 # Happy99 的帖子

恩,我也是在我的程序里直接提取的,没有改gf域的那个函数,多谢哈~~
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 18:41 , Processed in 0.067750 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表