matlab s变换

A4=readdata(‘E:\mydata.TXT‘);
[st,t,f] = st(A4(1:1000,2));

surf(t,f,10*log10(abs(st)),‘EdgeColor‘,‘none‘);

axis xy; axis tight; colormap(jet); view(0,90);

=============================Stockwell Transform=======================================

function [st,t,f] = st(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate)
% Returns the Stockwell Transform of the timeseries.
% Code by Robert Glenn Stockwell.
% DO NOT DISTRIBUTE
% BETA TEST ONLY
% Reference is "Localization of the Complex Spectrum: The S Transform"
% from IEEE Transactions on Signal Processing, vol. 44., number 4, April 1996, pages 998-1001.
%
%-------Inputs Needed------------------------------------------------

%   *****All frequencies in (cycles/(time unit))!******
% "timeseries" - vector of data to be transformed
%-------Optional Inputs ------------------------------------------------
%
%"minfreq" is the minimum frequency in the ST result(Default=0)
%"maxfreq" is the maximum frequency in the ST result (Default=Nyquist)
%"samplingrate" is the time interval between samples (Default=1)
%"freqsamplingrate" is the frequency-sampling interval you desire in the ST result (Default=1)
%Passing a negative number will give the default ex.  [s,t,f] = st(data,-1,-1,2,2)
%-------Outputs Returned------------------------------------------------
%
% st     -a complex matrix containing the Stockwell transform.
%    The rows of STOutput are the frequencies and the
%         columns are the time values ie each column is
%         the "local spectrum" for that point in time
%  t      - a vector containing the sampled times
%  f      - a vector containing the sampled frequencies
%--------Additional details-----------------------
%   %  There are several parameters immediately below that
%  the user may change. They are:
%[verbose]    if true prints out informational messages throughout the function.
%[removeedge] if true, removes a least squares fit parabola
%                and puts a 5% hanning taper on the edges of the time series.
%                This is usually a good idea.
%[analytic_signal]  if the timeseries is real-valued
%                      this takes the analytic signal and STs it.
%                      This is almost always a good idea.
%[factor]     the width factor of the localizing gaussian
%                ie, a sinusoid of period 10 seconds has a
%                gaussian window of width factor*10 seconds.
%                I usually use factor=1, but sometimes factor = 3
%                to get better frequency resolution.
%   Copyright (c) by Bob Stockwell
%   $Revision: 1.2 $  $Date: 1997/07/08  $

% This is the S transform wrapper that holds default values for the function.
TRUE = 1;
FALSE = 0;
%%% DEFAULT PARAMETERS  [change these for your particular application]
verbose = TRUE;         
removeedge= FALSE;
analytic_signal =  FALSE;
factor = 1;
%%% END of DEFAULT PARAMETERS

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

if verbose disp(‘ ‘),end  % i like a line left blank

if nargin == 0
   if verbose disp(‘No parameters inputted.‘),end
   st_help
   t=0;,st=-1;,f=0;
   return
end

% Change to column vector
if size(timeseries,2) > size(timeseries,1)
 timeseries=timeseries‘; 
end

% Make sure it is a 1-dimensional array
if size(timeseries,2) > 1
   error(‘Please enter a *vector* of data, not matrix‘)
 return
elseif (size(timeseries)==[1 1]) == 1
 error(‘Please enter a *vector* of data, not a scalar‘)
 return
end

% use defaults for input variables

if nargin == 1
   minfreq = 0;
   maxfreq = fix(length(timeseries)/2);
   samplingrate=1;
   freqsamplingrate=1;
elseif nargin==2
   maxfreq = fix(length(timeseries)/2);
   samplingrate=1;
   freqsamplingrate=1;
   [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
elseif nargin==3
   samplingrate=1;
   freqsamplingrate=1;
   [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
elseif nargin==4  
   freqsamplingrate=1;
   [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
elseif nargin == 5
      [ minfreq,maxfreq,samplingrate,freqsamplingrate] =  check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
else     
   if verbose disp(‘Error in input arguments: using defaults‘),end
   minfreq = 0;
   maxfreq = fix(length(timeseries)/2);
   samplingrate=1;
   freqsamplingrate=1;
end
if verbose
   disp(sprintf(‘Minfreq = %d‘,minfreq))
   disp(sprintf(‘Maxfreq = %d‘,maxfreq))
   disp(sprintf(‘Sampling Rate (time   domain) = %d‘,samplingrate))
   disp(sprintf(‘Sampling Rate (freq.  domain) = %d‘,freqsamplingrate))
   disp(sprintf(‘The length of the timeseries is %d points‘,length(timeseries)))

disp(‘ ‘)
end
%END OF INPUT VARIABLE CHECK

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

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

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

%WRITE switch statement on nargout
% if 0 then plot amplitude spectrum
if nargout==0
   if verbose disp(‘Plotting pseudocolor image‘),end
   pcolor(t,f,abs(st))
end

return

%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

% Compute the length of the data.
n=length(timeseries);
original = timeseries;
if removeedge
    if verbose disp(‘Removing trend with polynomial fit‘),end
   ind = [0:n-1]‘;
    r = polyfit(ind,timeseries,2);
    fit = polyval(r,ind) ;
  timeseries = timeseries - fit;
    if verbose disp(‘Removing edges with 5% hanning taper‘),end
    sh_len = floor(length(timeseries)/10);
    wn = hanning(sh_len);
    if(sh_len==0)
       sh_len=length(timeseries);
       wn = 1&[1:sh_len];
    end
    % make sure wn is a column vector, because timeseries is
   if size(wn,2) > size(wn,1)
      wn=wn‘; 
   end
  
   timeseries(1:floor(sh_len/2),1) = timeseries(1:floor(sh_len/2),1).*wn(1:floor(sh_len/2),1);
 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);
 
end

% If vector is real, do the analytic signal

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

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

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

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

%%% end strans function

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

% Function to compute the Gaussion window for
% function Stransform. g_window is used by function
% Stransform. Programmed by Eric Tittley
%
%-----Inputs Needed--------------------------
%
% length-the length of the Gaussian window
%
% freq-the frequency at which to evaluate
%    the window.
% factor- the window-width factor
%
%-----Outputs Returned--------------------------
%
% gauss-The Gaussian window
%

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

%-----------------------------------------------------------------------

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

% if the parameters are passed as an array, put them into the appropriate variables
s = size(minfreq);
l = max(s);
if l > 1 
   if verbose disp(‘Array of inputs accepted.‘),end
   temp=minfreq;
   minfreq = temp(1);;
   if l > 1  maxfreq = temp(2);,end;
   if l > 2  samplingrate = temp(3);,end;
   if l > 3  freqsamplingrate = temp(4);,end;
   if l > 4 
      if verbose disp(‘Ignoring extra input parameters.‘),end
   end;

end     
    
   if minfreq < 0 | minfreq > fix(length(timeseries)/2);
      minfreq = 0;
      if verbose disp(‘Minfreq < 0 or > Nyquist. Setting minfreq = 0.‘),end
   end
   if maxfreq > length(timeseries)/2  | maxfreq < 0
      maxfreq = fix(length(timeseries)/2);
      if verbose disp(sprintf(‘Maxfreq < 0 or > Nyquist. Setting maxfreq = %d‘,maxfreq)),end
   end
      if minfreq > maxfreq
      temporary = minfreq;
      minfreq = maxfreq;
      maxfreq = temporary;
      clear temporary;
      if verbose disp(‘Swapping maxfreq <=> minfreq.‘),end
   end
   if samplingrate <0
      samplingrate = abs(samplingrate);
      if verbose disp(‘Samplingrate <0. Setting samplingrate to its absolute value.‘),end
   end
   if freqsamplingrate < 0   % check ‘what if freqsamplingrate > maxfreq - minfreq‘ case
      freqsamplingrate = abs(freqsamplingrate);
      if verbose disp(‘Frequency Samplingrate negative, taking absolute value‘),end
   end

% bloody odd how you don‘t end a function

%^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^%
function st_help
   disp(‘ ‘)
 disp(‘st()  HELP COMMAND‘)
 disp(‘st() returns  - 1 or an error message if it fails‘)
 disp(‘USAGE::    [localspectra,timevector,freqvector] = st(timeseries)‘)
   disp(‘NOTE::   The function st() sets default parameters then calls the function strans()‘)
   disp(‘ ‘) 
   disp(‘You can call strans() directly and pass the following parameters‘)
   disp(‘ **** Warning!  These inputs are not checked if strans() is called directly!! ****‘)
   disp(‘USAGE::  localspectra = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor) ‘)
    
   disp(‘ ‘)
   disp(‘Default parameters (available in st.m)‘)
 disp(‘VERBOSE          - prints out informational messages throughout the function.‘)
 disp(‘REMOVEEDGE       - removes the edge with a 5% taper, and takes‘)
   disp(‘FACTOR           -  the width factor of the localizing gaussian‘)
   disp(‘                    ie, a sinusoid of period 10 seconds has a ‘)
   disp(‘                    gaussian window of width factor*10 seconds.‘)
   disp(‘                    I usually use factor=1, but sometimes factor = 3‘)
   disp(‘                    to get better frequency resolution.‘)
   disp(‘ ‘)
   disp(‘Default input variables‘)
   disp(‘MINFREQ           - the lowest frequency in the ST result(Default=0)‘)
   disp(‘MAXFREQ           - the highest frequency in the ST result (Default=nyquist‘)
   disp(‘SAMPLINGRATE      - the time interval between successive data points (Default = 1)‘)
   disp(‘FREQSAMPLINGRATE  - the number of frequencies between samples in the ST results‘)
 
% end of st_help procedure

时间: 2024-08-18 04:40:39

matlab s变换的相关文章

matlab练习程序(矩形变换为单连通形状)

变换使用的模板必须是单连通的,如果在模板中打个结,这里的程序就处理不了了. 虽然非单连通模板也有办法处理,不过不是这里要讨论的. 这里用到的方法和矩形变换为圆那片文章中用的方法几乎一样,变换前后像素按比例缩减,不过在判断弧度和图像边界到模板中心距离时略有不同. 变换为圆时弧度可以直接计算出来,而变换为任意形状只能算出一个最小相似值. 至于图像边界到模板中心距离只能分八种情况判断了,处理圆时可以根据对称性简化程序,这里似乎没有什么好办法简化. 变换细节上,那篇文章中使用的是正向插值,这里使用正向插

matlab练习程序(矩形变换为圆)

最近对图像坐标的变换很感兴趣啊,这次是将一张图像变换为圆形. 变换原理就是按变换前后像素到圆心的距离按比例缩减就行了. 改变x,y方向上的系数,应该还可以变换为椭圆,不过我还没有尝试. 注意我这里相当于缩小图像了,所以用的是正向插值,如果想生成一个大圆,还是需要逆向插值的. 原图如下: 处理后效果: matlab代码如下: clear all;close all;clc; img=imread('lena.jpg'); [h w]=size(img); imshow(img); imgn=zer

matlab学习笔记第九章——变换

1.拉普拉斯变换:时间函数f(t)的拉普拉斯变换用下面的积分式定义: L {f(t)} = ??0∞f(t)e-stdt,我们通常把f(t)的拉普拉斯变换写F(s).在MATLAB计算拉普拉斯变换,我们要调用laplace(f(t)),它做的是符号计算. L (tn) = n!/sn+1 要计算拉普拉斯逆变换,我们输入ilaplace 2.一个函数f(t)的傅立叶变换被定义为: F(ω) = ??-∞∞f(t)e-iωtdt 在MATLAB中我们可以输入fourier命令计算一个函数的傅立叶变换

MATLAB数字图像处理(一)基础操作和傅立叶变换

数字图像处理是一门集计算机科学.光学.数学.物理学等多学科的综合科学.随着计算机科学的发展,数字图像处理技术取得了巨大的进展,呈现出强大的生命力,已经在多种领域取得了大量的应用,推动了社会的发展.其中,遥感领域中,对于影像数据的处理均基于数字图像处理的技术.而遥感影像数据作为地理信息科学的重要数据源,如何从中获取有用的信息,是地理信息数据处理中重要的内容. MATLAB作为数学领域应用最广泛的一种软件,集成了对于图片处理的函数和功能,成为了处理数字图像问题的佼佼者.其出众的计算能力和简便的绘图能

为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换

写在最前面:本文是我阅读了多篇相关文章后对它们进行分析重组整合而得,绝大部分内容非我所原创.在此向多位原创作者致敬!!! 一.傅立叶变换的由来 关于傅立叶变换,无论是书本还是在网上可以很容易找到关于傅立叶变换的描述,但是大都是些故弄玄虚的文章,太过抽象,尽是一些让人看了就望而生畏的公式的罗列,让人很难能够从感性上得到理解,最近,我偶尔从网上看到一个关于数字信号处理的电子书籍,是一个叫Steven W. Smith, Ph.D.外国人写的,写得非常浅显,里面有七章由浅入深地专门讲述关于离散信号的傅

Matlab实现Hough变换检测图像中的直线

Hough变换的原理: 将图像从图像空间变换至参数空间,变换公式如下: 变换以后,图像空间与参数空间存在以下关系: 图像空间中的一点在参数空间是一条曲线,而图像空间共线的各点对应于参数空间交于一点的各条曲线. 下面使用Matlab实现Hough变换对图像中的直线划痕进行检测. close all; clear all; I = imread('scratch.tif'); figure; subplot(1,3,1); imshow(I); BW = edge(I,'canny');%Canny

Matlab图像处理系列4———图像傅立叶变换与反变换

注:本系列来自于图像处理课程实验,用Matlab实现最基本的图像处理算法 1.Fourier变换 (1)频域增强 除了在空间域内可以加工处理图像以外,我们还可以将图像变换到其他空间后进行处理,这些方法称为变换域方法,最常见的变换域是频域. 使用Fourier变换把图像从空间域变换到频域,在频域内做相应增强处理,再从频域变换到空间域得到处理后的图像. 我们这里主要学习Fourier变换和FFT变换的算法,没有学过通信原理,我对信号.时域分析也不是很清楚. 2.FFT算法 (1)离散Fourier变

Hough变换检测圆(附:MATLAB程序) - mhjerry的专栏(子水) - 博客频道 - CSDN.NET

来源:http://blog.csdn.net/mhjerry/article/details/7061819#1536434-hi-1-45330-42d97150898b1af15ddaae52f91f09c2 Hough变换很好玩,以前在学校写过一些检测圆圈.椭圆.双曲线等图像,同时也可以检测多个圆形.

MATLAB图像处理_直接操作像素点进行颜色变换

需求 直接操作RGB图像的像素点,进行颜色的相关操作. 掌握这个,必须对MATLAB中矩阵的操作有所熟悉,特别是整行.整列的操作. 如: J = [1 2 3; 4 5 6; 7 8 9]; --这里定义了一个三行三列的矩阵. J[:, 1] = 0; --直接操作了J矩阵中每一行的第1列 此时J = [0 2 3; 0 5 6; 0 8 9] 其他如行操作用法类似,不再赘述. 下面我们对一副图像进行直接操作,把其中的红色部分改为蓝色. 代码如下: % BY SCOTT % red2blue %