当前位置:   article > 正文

一维时间序列信号的广义傅里叶族变换(Matlab)

一维时间序列信号的广义傅里叶族变换(Matlab)

广义傅里叶族变换是一种时频变换方法,傅里叶变换、短时傅里叶变换、S变换和许多小波变换都是其特殊情况,完整代码及子函数如下,很容易读懂:

  1. % Run a demo by creating a signal, transforming it, and plotting the results
  2. % Create a fake signal
  3. N = 256;
  4. x = linspace(0,1,N);
  5. sig = zeros(1,length(x));
  6. % signal example 1 (a single delta)
  7. sig(N/2) = 1.0;
  8. % signal example 2 (a mixture of sinusoids and a delta)
  9. % sig(1:N/2) += (sin((N/16)*2*pi*x)*1.0)(1:N/2);
  10. % sig(N/2+1:N) += (cos((N/8)*2*pi*x)*1.0)(N/2+1:N);
  11. % sig(2*N/16+1:3*N/16) += (sin((N/4)*2*pi*x)*1.0)(2*N/16+1:3*N/16);
  12. % sig(N/2+N/4+1) = 2.0;
  13. % Do the transform
  14. partitions = octavePartitions(N);
  15. windows = boxcarWindows(partitions);
  16. SIG = GFT(sig,partitions,windows);
  17. % Interpolate to get a spectrogram
  18. % The third and fourth parameters set the time and frequency axes respectively,
  19. % and can be changed to raise or lower the resolution, or zoom in on
  20. % a feature of interest
  21. spectrogram = interpolateGFT(SIG,partitions,1024,1024);
  22. % Display
  23. figure();
  24. subplot(3,1,1);
  25. plot(x,sig,'DisplayName','signal');
  26. legend('Location','northeast')
  27. ax = subplot(3,1,2);
  28. hold on;
  29. for p = partitions
  30. line([x(p),x(p)],[0,max(abs(SIG))],'Color',[1 0 0],'linestyle','--');
  31. end
  32. p = plot(x,abs(SIG),'DisplayName','SIGNAL');
  33. legend(p,'Location','northeast');
  34. subplot(3,1,3);
  35. imagesc(abs(spectrogram));
  36. %%
  37. function partitions = octavePartitions(N)
  38. widths = 2.^(0:round(log(N)/log(2)-2));
  39. widths = [1,widths,flip(widths)];
  40. partitions = [0,cumsum(widths)]+1;
  41. end
  42. %%
  43. function widths = partitionWidths(partitions)
  44. widths = circshift(partitions,-1) - partitions;
  45. widths(length(partitions)) = widths(length(partitions)) + max(partitions);
  46. end
  47. %%
  48. function windows = boxcarWindows(partitions)
  49. windows = ones(1,max(partitions));
  50. end
  51. %%
  52. function SIG = GFT(sig,partitions,windows)
  53. SIG = fft(complex(sig));
  54. SIG = SIG.*windows;
  55. for p = 1:(length(partitions)-1)
  56. SIG(partitions(p):partitions(p+1)-1) = ifft(SIG(partitions(p):partitions(p+1)-1));
  57. end
  58. end
  59. %%
  60. function spectrogram = interpolateGFT(SIG,partitions,tAxis,fAxis,method)
  61. % Interpolate a 1D GFT onto a grid. If axes is specified it should be a
  62. % list or tuple consisting of two arrays, the sampling points on the time and frequency
  63. % axes, respectively. Alternatively, M can be specified, which gives the number
  64. % of points along each axis.
  65. % introduced in R2019 is the arguments block
  66. % https://www.mathworks.com/help/matlab/ref/arguments.html
  67. % arguments
  68. % SIG;
  69. % partitions;
  70. % tAxis;
  71. % fAxis;
  72. % method (1,:) char = 'linear';
  73. % end
  74. % if you don't have have the arguments block, then you can still do input defaults like this:
  75. if nargin<5
  76. method = 'linear';
  77. end
  78. % Caller specified M rather than the actual sampling points
  79. if length(tAxis) == 1
  80. tAxis = 1:length(SIG) / tAxis:length(SIG);
  81. % Centre the samples
  82. tAxis = tAxis + (length(SIG) - tAxis(length(tAxis))) / 2;
  83. end
  84. if length(fAxis) == 1
  85. fAxis = 1:length(SIG) / fAxis:length(SIG);
  86. % Centre the samples
  87. fAxis = fAxis + (length(SIG) - fAxis(length(fAxis))) / 2;
  88. end
  89. N = length(SIG);
  90. widths = partitionWidths(partitions);
  91. spectrogram = complex(length(partitions),zeros(length(tAxis)));
  92. % interpolate each frequency band in time
  93. for p = 1:length(partitions)
  94. % indices of sample points, plus 3 extra on each side in case of cubic interpolation
  95. indices = (-3:widths(p)+2);
  96. % time coordinates of samples
  97. t = indices .* (N/widths(p));
  98. % values at sample points
  99. if (p < length(partitions))
  100. temp = SIG(partitions(p):partitions(p+1)-1);
  101. f = temp(mod(indices,widths(p))+1);
  102. else
  103. temp = SIG(partitions(p):N);
  104. f = temp(mod(indices,widths(p))+1);
  105. end
  106. if (length(f) > 1)
  107. spectrogram(p,:) = interp1(t,f,tAxis,method);
  108. else
  109. spectrogram(p,:) = f;
  110. end
  111. end
  112. % Interpolate in frequency
  113. indices = mod(-3:length(partitions)+2,length(partitions));
  114. f = partitions(indices+1) + widths(indices+1)/2;
  115. f(1:3) = f(1:3) - N;
  116. f(length(f)-2:length(f)) = f(length(f)-2:length(f)) + N;
  117. t = spectrogram(indices+1,:);
  118. spectrogram = interp1(f,t,fAxis,method);
  119. end
  120. function [sig,partitions,windows,SIG] = demo()
  121. % Run a demo by creating a signal, transforming it, and plotting the results
  122. % Create a fake signal
  123. N = 256;
  124. x = linspace(0,1,N);
  125. sig = zeros(1,length(x));
  126. % signal example 1 (a single delta)
  127. sig(N/2) = 1.0;
  128. % signal example 2 (a mixture of sinusoids and a delta)
  129. % sig(1:N/2) += (sin((N/16)*2*pi*x)*1.0)(1:N/2);
  130. % sig(N/2+1:N) += (cos((N/8)*2*pi*x)*1.0)(N/2+1:N);
  131. % sig(2*N/16+1:3*N/16) += (sin((N/4)*2*pi*x)*1.0)(2*N/16+1:3*N/16);
  132. % sig(N/2+N/4+1) = 2.0;
  133. % Do the transform
  134. partitions = octavePartitions(N);
  135. windows = boxcarWindows(partitions);
  136. SIG = GFT(sig,partitions,windows);
  137. % Interpolate to get a spectrogram
  138. % The third and fourth parameters set the time and frequency axes respectively,
  139. % and can be changed to raise or lower the resolution, or zoom in on
  140. % a feature of interest
  141. spectrogram = interpolateGFT(SIG,partitions,1024,1024);
  142. % Display
  143. figure();
  144. subplot(3,1,1);
  145. plot(x,sig,'DisplayName','signal');
  146. legend('Location','northeast')
  147. ax = subplot(3,1,2);
  148. hold on;
  149. for p = partitions
  150. line([x(p),x(p)],[0,max(abs(SIG))],'Color',[1 0 0],'linestyle','--');
  151. end
  152. p = plot(x,abs(SIG),'DisplayName','SIGNAL');
  153. legend(p,'Location','northeast');
  154. subplot(3,1,3);
  155. imagesc(abs(spectrogram));
  156. end

图片

工学博士,担任《Mechanical System and Signal Processing》《中国电机工程学报》《控制与决策》等期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/705422
推荐阅读
相关标签
  

闽ICP备14008679号