声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 5345|回复: 7

[混合编程] 求广义S变换的程序

[复制链接]
发表于 2010-9-15 16:56 | 显示全部楼层 |阅读模式

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

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

x
求广义S变换的MATLAB程序,先谢啦
回复
分享到:

使用道具 举报

发表于 2010-9-15 22:29 | 显示全部楼层
  借你的帖子同求!
发表于 2010-9-15 23:56 | 显示全部楼层
不懂! 但google一下不是就有了:@)
http://www.cora.nwra.com/~stockw ... _page&PAGE_id=9

  1. function [st,t,f] = st(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate)
  2. % Returns the Stockwell Transform of the timeseries.
  3. % Code by Robert Glenn Stockwell.
  4. % DO NOT DISTRIBUTE
  5. % BETA TEST ONLY
  6. % Reference is "Localization of the Complex Spectrum: The S Transform"
  7. % from IEEE Transactions on Signal Processing, vol. 44., number 4, April 1996, pages 998-1001.
  8. %
  9. %-------Inputs Needed------------------------------------------------
  10. %  
  11. %   *****All frequencies in (cycles/(time unit))!******
  12. %        "timeseries" - vector of data to be transformed
  13. %-------Optional Inputs ------------------------------------------------
  14. %
  15. %"minfreq" is the minimum frequency in the ST result(Default=0)
  16. %"maxfreq" is the maximum frequency in the ST result (Default=Nyquist)
  17. %"samplingrate" is the time interval between samples (Default=1)
  18. %"freqsamplingrate" is the frequency-sampling interval you desire in the ST result (Default=1)
  19. %Passing a negative number will give the default ex.  [s,t,f] = st(data,-1,-1,2,2)
  20. %-------Outputs Returned------------------------------------------------
  21. %
  22. % st     -a complex matrix containing the Stockwell transform.
  23. %                         The rows of STOutput are the frequencies and the
  24. %         columns are the time values ie each column is
  25. %         the "local spectrum" for that point in time
  26. %  t      - a vector containing the sampled times
  27. %  f      - a vector containing the sampled frequencies
  28. %--------Additional details-----------------------
  29. %   %  There are several parameters immediately below that
  30. %  the user may change. They are:
  31. %[verbose]    if true prints out informational messages throughout the function.
  32. %[removeedge] if true, removes a least squares fit parabola
  33. %                and puts a 5% hanning taper on the edges of the time series.
  34. %                This is usually a good idea.
  35. %[analytic_signal]  if the timeseries is real-valued
  36. %                      this takes the analytic signal and STs it.
  37. %                      This is almost always a good idea.
  38. %[factor]     the width factor of the localizing gaussian
  39. %                ie, a sinusoid of period 10 seconds has a
  40. %                gaussian window of width factor*10 seconds.
  41. %                I usually use factor=1, but sometimes factor = 3
  42. %                to get better frequency resolution.
  43. %   Copyright (c) by Bob Stockwell
  44. %   $Revision: 1.2 $  $Date: 1997/07/08  $


  45. % This is the S transform wrapper that holds default values for the function.
  46. TRUE = 1;
  47. FALSE = 0;
  48. %%% DEFAULT PARAMETERS  [change these for your particular application]
  49. verbose = TRUE;         
  50. removeedge= FALSE;
  51. analytic_signal =  FALSE;
  52. factor = 1;
  53. %%% END of DEFAULT PARAMETERS


  54. %%%START OF INPUT VARIABLE CHECK
  55. % First:  make sure it is a valid time_series
  56. %         If not, return the help message

  57. if verbose disp(' '),end  % i like a line left blank

  58. if nargin == 0
  59.    if verbose disp('No parameters inputted.'),end
  60.    st_help
  61.    t=0;,st=-1;,f=0;
  62.    return
  63. end

  64. % Change to column vector
  65. if size(timeseries,2) > size(timeseries,1)
  66.         timeseries=timeseries';       
  67. end

  68. % Make sure it is a 1-dimensional array
  69. if size(timeseries,2) > 1
  70.    error('Please enter a *vector* of data, not matrix')
  71.         return
  72. elseif (size(timeseries)==[1 1]) == 1
  73.         error('Please enter a *vector* of data, not a scalar')
  74.         return
  75. end

  76. % use defaults for input variables

  77. if nargin == 1
  78.    minfreq = 0;
  79.    maxfreq = fix(length(timeseries)/2);
  80.    samplingrate=1;
  81.    freqsamplingrate=1;
  82. elseif nargin==2
  83.    maxfreq = fix(length(timeseries)/2);
  84.    samplingrate=1;
  85.    freqsamplingrate=1;
  86.    [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
  87. elseif nargin==3
  88.    samplingrate=1;
  89.    freqsamplingrate=1;
  90.    [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
  91. elseif nargin==4   
  92.    freqsamplingrate=1;
  93.    [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
  94. elseif nargin == 5
  95.       [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
  96. else      
  97.    if verbose disp('Error in input arguments: using defaults'),end
  98.    minfreq = 0;
  99.    maxfreq = fix(length(timeseries)/2);
  100.    samplingrate=1;
  101.    freqsamplingrate=1;
  102. end
  103. if verbose
  104.    disp(sprintf('Minfreq = %d',minfreq))
  105.    disp(sprintf('Maxfreq = %d',maxfreq))
  106.    disp(sprintf('Sampling Rate (time   domain) = %d',samplingrate))
  107.    disp(sprintf('Sampling Rate (freq.  domain) = %d',freqsamplingrate))
  108.    disp(sprintf('The length of the timeseries is %d points',length(timeseries)))

  109.    disp(' ')
  110. end
  111. %END OF INPUT VARIABLE CHECK

  112. % If you want to "hardwire" minfreq & maxfreq & samplingrate & freqsamplingrate do it here

  113. % calculate the sampled time and frequency values from the two sampling rates
  114. t = (0:length(timeseries)-1)*samplingrate;
  115. spe_nelements =ceil((maxfreq - minfreq+1)/freqsamplingrate)   ;
  116. f = (minfreq + [0:spe_nelements-1]*freqsamplingrate)/(samplingrate*length(timeseries));
  117. if verbose disp(sprintf('The number of frequency voices is %d',spe_nelements)),end


  118. % The actual S Transform function is here:
  119. st = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor);
  120. % this function is below, thus nicely encapsulated

  121. %WRITE switch statement on nargout
  122. % if 0 then plot amplitude spectrum
  123. if nargout==0
  124.    if verbose disp('Plotting pseudocolor image'),end
  125.    pcolor(t,f,abs(st))
  126. end


  127. return


  128. %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  129. %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  130. %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  131. %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  132. %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


  133. function st = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor);
  134. % Returns the Stockwell Transform, STOutput, of the time-series
  135. % Code by R.G. Stockwell.
  136. % Reference is "Localization of the Complex Spectrum: The S Transform"
  137. % from IEEE Transactions on Signal Processing, vol. 44., number 4,
  138. % April 1996, pages 998-1001.
  139. %
  140. %-------Inputs Returned------------------------------------------------
  141. %         - are all taken care of in the wrapper function above
  142. %
  143. %-------Outputs Returned------------------------------------------------
  144. %
  145. %        ST    -a complex matrix containing the Stockwell transform.
  146. %                         The rows of STOutput are the frequencies and the
  147. %                         columns are the time values
  148. %
  149. %
  150. %-----------------------------------------------------------------------

  151. % Compute the length of the data.
  152. n=length(timeseries);
  153. original = timeseries;
  154. if removeedge
  155.     if verbose disp('Removing trend with polynomial fit'),end
  156.          ind = [0:n-1]';
  157.     r = polyfit(ind,timeseries,2);
  158.     fit = polyval(r,ind) ;
  159.          timeseries = timeseries - fit;
  160.     if verbose disp('Removing edges with 5% hanning taper'),end
  161.     sh_len = floor(length(timeseries)/10);
  162.     wn = hanning(sh_len);
  163.     if(sh_len==0)
  164.        sh_len=length(timeseries);
  165.        wn = 1&[1:sh_len];
  166.     end
  167.     % make sure wn is a column vector, because timeseries is
  168.    if size(wn,2) > size(wn,1)
  169.       wn=wn';       
  170.    end
  171.    
  172.    timeseries(1:floor(sh_len/2),1) = timeseries(1:floor(sh_len/2),1).*wn(1:floor(sh_len/2),1);
  173.         timeseries(length(timeseries)-floor(sh_len/2):n,1) = timeseries(length(timeseries)-floor(sh_len/2):n,1).*wn(sh_len-floor(sh_len/2):sh_len,1);
  174.   
  175. end

  176. % If vector is real, do the analytic signal

  177. if analytic_signal
  178.    if verbose disp('Calculating analytic signal (using Hilbert transform)'),end
  179.    % this version of the hilbert transform is different than hilbert.m
  180.    %  This is correct!
  181.    ts_spe = fft(real(timeseries));
  182.    h = [1; 2*ones(fix((n-1)/2),1); ones(1-rem(n,2),1); zeros(fix((n-1)/2),1)];
  183.    ts_spe(:) = ts_spe.*h(:);
  184.    timeseries = ifft(ts_spe);
  185. end  

  186. % Compute FFT's
  187. tic;vector_fft=fft(timeseries);tim_est=toc;
  188. vector_fft=[vector_fft,vector_fft];
  189. tim_est = tim_est*ceil((maxfreq - minfreq+1)/freqsamplingrate)   ;
  190. if verbose disp(sprintf('Estimated time is %f',tim_est)),end

  191. % Preallocate the STOutput matrix
  192. st=zeros(ceil((maxfreq - minfreq+1)/freqsamplingrate),n);
  193. % Compute the mean
  194. % Compute S-transform value for 1 ... ceil(n/2+1)-1 frequency points
  195. if verbose disp('Calculating S transform...'),end
  196. if minfreq == 0
  197.    st(1,:) = mean(timeseries)*(1&[1:1:n]);
  198. else
  199.           st(1,:)=ifft(vector_fft(minfreq+1:minfreq+n).*g_window(n,minfreq,factor));
  200. end

  201. %the actual calculation of the ST
  202. % Start loop to increment the frequency point
  203. for banana=freqsamplingrate:freqsamplingrate:(maxfreq-minfreq)
  204.    st(banana/freqsamplingrate+1,:)=ifft(vector_fft(minfreq+banana+1:minfreq+banana+n).*g_window(n,minfreq+banana,factor));
  205. end   % a fruit loop!   aaaaa ha ha ha ha ha ha ha ha ha ha
  206. % End loop to increment the frequency point
  207. if verbose disp('Finished Calculation'),end

  208. %%% end strans function

  209. %------------------------------------------------------------------------
  210. function gauss=g_window(length,freq,factor)

  211. % Function to compute the Gaussion window for
  212. % function Stransform. g_window is used by function
  213. % Stransform. Programmed by Eric Tittley
  214. %
  215. %-----Inputs Needed--------------------------
  216. %
  217. %        length-the length of the Gaussian window
  218. %
  219. %        freq-the frequency at which to evaluate
  220. %                  the window.
  221. %        factor- the window-width factor
  222. %
  223. %-----Outputs Returned--------------------------
  224. %
  225. %        gauss-The Gaussian window
  226. %

  227. vector(1,:)=[0:length-1];
  228. vector(2,:)=[-length:-1];
  229. vector=vector.^2;   
  230. vector=vector*(-factor*2*pi^2/freq^2);
  231. % Compute the Gaussion window
  232. gauss=sum(exp(vector));

  233. %-----------------------------------------------------------------------

  234. %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^%
  235. function [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries)
  236. % this checks numbers, and replaces them with defaults if invalid

  237. % if the parameters are passed as an array, put them into the appropriate variables
  238. s = size(minfreq);
  239. l = max(s);
  240. if l > 1  
  241.    if verbose disp('Array of inputs accepted.'),end
  242.    temp=minfreq;
  243.    minfreq = temp(1);;
  244.    if l > 1  maxfreq = temp(2);,end;
  245.    if l > 2  samplingrate = temp(3);,end;
  246.    if l > 3  freqsamplingrate = temp(4);,end;
  247.    if l > 4  
  248.       if verbose disp('Ignoring extra input parameters.'),end
  249.    end;

  250. end      
  251.      
  252.    if minfreq < 0 | minfreq > fix(length(timeseries)/2);
  253.       minfreq = 0;
  254.       if verbose disp('Minfreq < 0 or > Nyquist. Setting minfreq = 0.'),end
  255.    end
  256.    if maxfreq > length(timeseries)/2  | maxfreq < 0
  257.       maxfreq = fix(length(timeseries)/2);
  258.       if verbose disp(sprintf('Maxfreq < 0 or > Nyquist. Setting maxfreq = %d',maxfreq)),end
  259.    end
  260.       if minfreq > maxfreq
  261.       temporary = minfreq;
  262.       minfreq = maxfreq;
  263.       maxfreq = temporary;
  264.       clear temporary;
  265.       if verbose disp('Swapping maxfreq <=> minfreq.'),end
  266.    end
  267.    if samplingrate <0
  268.       samplingrate = abs(samplingrate);
  269.       if verbose disp('Samplingrate <0. Setting samplingrate to its absolute value.'),end
  270.    end
  271.    if freqsamplingrate < 0   % check 'what if freqsamplingrate > maxfreq - minfreq' case
  272.       freqsamplingrate = abs(freqsamplingrate);
  273.       if verbose disp('Frequency Samplingrate negative, taking absolute value'),end
  274.    end

  275. % bloody odd how you don't end a function

  276. %^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^%
  277. function st_help
  278.    disp(' ')
  279.         disp('st()  HELP COMMAND')
  280.         disp('st() returns  - 1 or an error message if it fails')
  281.         disp('USAGE::    [localspectra,timevector,freqvector] = st(timeseries)')
  282.           disp('NOTE::   The function st() sets default parameters then calls the function strans()')
  283.    disp(' ')  
  284.    disp('You can call strans() directly and pass the following parameters')
  285.    disp(' **** Warning!  These inputs are not checked if strans() is called directly!! ****')
  286.           disp('USAGE::  localspectra = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor) ')
  287.      
  288.    disp(' ')
  289.    disp('Default parameters (available in st.m)')
  290.         disp('VERBOSE          - prints out informational messages throughout the function.')
  291.         disp('REMOVEEDGE       - removes the edge with a 5% taper, and takes')
  292.    disp('FACTOR           -  the width factor of the localizing gaussian')
  293.    disp('                    ie, a sinusoid of period 10 seconds has a ')
  294.    disp('                    gaussian window of width factor*10 seconds.')
  295.    disp('                    I usually use factor=1, but sometimes factor = 3')
  296.    disp('                    to get better frequency resolution.')
  297.    disp(' ')
  298.    disp('Default input variables')
  299.    disp('MINFREQ           - the lowest frequency in the ST result(Default=0)')
  300.    disp('MAXFREQ           - the highest frequency in the ST result (Default=nyquist')
  301.    disp('SAMPLINGRATE      - the time interval between successive data points (Default = 1)')
  302.    disp('FREQSAMPLINGRATE  - the number of frequencies between samples in the ST results')
  303.        
  304. % end of st_help procedure   
复制代码

  1. % the s-tranform test script
  2. len = 128;
  3. freq = 5;   
  4. t = 0:len-1;

  5. % CREATE CROSS CHIRP TIME SERIES
  6. cross_chirp = cos(2*pi*(10+t/7).*t/len) + cos(2*pi*(len/2.8-t/6.0).*t/len);
  7. % CREATE MODULATED SIN FUNCTION TIME SERIES
  8. mod_freq=4*cos(2*pi*t/len)+len/5;
  9. sin_of_sin = cos(2*pi*mod_freq.*t/len);
  10. % CREATE the Distinct sinusoids example
  11. midfreq = 20;
  12. lowfreq = 5;
  13. highfreq = 45;
  14. distinct = cos(2*pi*midfreq.*t/len);
  15. distinct(1:len/2) = cos(2*pi*lowfreq.*t(1:len/2)/len);
  16. distinct(20:30) = cos(2*pi*highfreq .*t(20:30)/len);
  17. % CREATE the chirp example
  18. chirp = cos(2*pi*(10+t/7).*t/len);



  19. [st_matrix,st_times,st_frequencies] = st(sin_of_sin);
  20. [st_matrix_chirp,st_times,st_frequencies] = st(chirp);
  21. [st_matrix_chirps,st_times,st_frequencies] = st(cross_chirp);
  22. [st_matrix_distinct,st_times,st_frequencies] = st(distinct);

  23. contourf(st_times,st_frequencies,abs(st_matrix_chirps));
  24. %mesh(abs(st_matrix_chirp));
复制代码
 楼主| 发表于 2010-9-16 09:50 | 显示全部楼层
谢谢,
请问运行时提示function [st,t,f] = st(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate)
   
Error: Function definitions are not permitted at the prompt or in scripts.
是什么意思,已安装时频分析软件包,还要安装其他软件包吗?


   
发表于 2010-9-16 12:38 | 显示全部楼层
回复 c2019 的帖子
LS直接复制至命令窗? 学下函数使用:@)
 楼主| 发表于 2010-9-18 10:08 | 显示全部楼层
谢谢
   
发表于 2012-4-10 20:51 | 显示全部楼层
求解广义S变换  扣扣1733726172
发表于 2012-10-31 11:39 | 显示全部楼层

上面的变换是广义S变换??、还是S变换?我只想用S变换输出频率和时间的曲线,谢谢
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-11-29 03:54 , Processed in 0.067620 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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