calculate discrete S transform for given discrete time series - matlab

let us consider following Page :
http://djj.ee.ntu.edu.tw/S_Transform.pdf
paragraph 2.3 The Discrete S Transform
let say that we have sampled version of signal x, and given sampling frequency fs,i have calculated discrete Fourier transform using following code
function y=DFT(x);
N=length(x);
D=zeros(N,N);
for k=1:N
for n=1: N
D(k,n)=exp((-j*(k-1)*2*pi*(n-1))/N);
end
end
y=D*x'/N;
end
and started to estimate discrete S transform
function [S]=discrete_s_transform(x,fs);
%compute discrete s transform
%fs-sampling frequency
N=length(x); % length of signal
T=1/fs; % sampling period
Y=DFT(X);
how can i continue related to this part ?
clearly loops are not problem to implement,just they go from 1 to N instead of 0 to N-1 because of matlab vectors are 1 based,but what about main code?multiplication to exponential? coudl you please help me to finish S transform ?

Your sum depends only on m so I assume that other parameters as well as function H((m+n)/(NT)) are defined. Via for-loops the simplest one is:
function [S]=discrete_s_transform(x);
N=length(x); % length of signal
S=0;
m=0;
for i=1:N
S=S+H((m+n)/(NT))*exp(a)*exp(b*m/N);
m=m+1;
end
Hope that helps!

Related

How can find the Fourier Coefficents of this DFT?

I have a data which contains 16 elements:
x=[8.57837e-08, 2.07482e-06, 4.43796e-06, 7.66462e-06, 1.10232e-05, 1.35811e-05, 1.27958e-05, 5.94217e-06, 2.49168e-08, -6.58389e-06, -1.30551e-05, -1.345e-05, -1.07471e-05, -7.38637e-06, -4.42876e-06, -1.88811e-06 ];
A = length(x)
I do DTFT-DFT like dirac signals:
n=0:A;
syms w
X_w=0;
for i=1:length(x)
X_w=X_w+x(i)*exp(-j*w*n(i));
end
figure;fplot(angle(X_w),[0 2*pi]),title('DTFT phase graph')
figure;fplot(abs(X_w),[0 2*pi]),title('DTFT amplitude graph')
hold on
%DFT
N=50;
k=0:N-1;
DFT_X=[];
for k=0:N-1
Xk=0;
for i=1:length(x)
Xk=Xk+x(i).*exp(-j*(2*pi/N).*k.*n(i));
end
DFT_X=[DFT_X Xk];
end
w=2*pi/N*(0:N-1);
stem(w,abs(DFT_X))`
The problem is I want to write this signal with cosinus and sinus components. But I don't really know how can I do.
Thank you all,
Emre.
Direct computation of the Fourier coefficients might be a better option than trying to relate the DFT to the DFS. Looking at the continuous time formulas:
all you'd need to do is sum the input signal x multiplied (element wise) by a cosine over it's domain for the real coefficient and sum the input signal x multiplied (element wise) by a sine over its domain for the imaginary coefficients.
Secondly, you could potentially use conjugate symmetry and these formulas to calculate the relationship between your Xk and the desired An and Bn coefficients.
Xk = (An-j*Bn)/2
and
Xk* = (An+j*Bn)/2)

Registration using particle filter in matlab

I wrote the following code as an implementation to the registration algorithm presented in an article named:"E. Arce-Santana, D. Campos-Delgado and A. Alba, Affine image registration guided by particle filter, IET Image Process. 6 (2012)".
Question 1: When I run the code no matter how many particles or iterations I choose, the output is still inaccurate. I do not know what is the problem?
Question 2: I commented a formula for updating the particle's weight, my question is, does I implement the equation write or the one that appears above it (which relied on the entropy) is the right one and therefore delete the commented one and leave the entropy based equation?
The code is as follow:
%% clear everything
clear all
close all
clc
%% Read the image data
% I1: Reference Image
I1=imread('cameraman.tif');
I1=double(imresize(I1,[256 256]));
figure,imshow(I1)
[I1r I1c I1d]=size(I1);
%I2: Target image
I2=randomtransform(I1); %user-defined function to generate random transformation
% I2=double(imresize(I2,[I1r I1c]));
figure,imshow(I2)
%% Particle Filter Steps:
%%%%% Input:
n=4; % Related to the initialization
m=256; % Related to the initialization
N=20; %(no. of iteration)
M=100; %(no. of particles)
phai=[]; %(state vector of the affine transformation parameters)
w=[]; %(the vector of the phai weights)
%k: iteration no.
%i: particle no.
Vk=1; % noise variance in the system (Used in the predection state)
w_v=1; % weights update equation related variance
beta=0.99; % annealing factor
%%%%% Output
phai_est=[]; %(Final estimated state vector of affine parameters)
%%%%% Steps:
%%% Step 1: Random generation of the particles
% The ranges are obtained from the paper
rotationAngle=double(int8(unifrnd(-pi/4,pi/4,1,1)));
scalingCoefX = double(unifrnd(0.5,2,1,1));
scalingCoefY = double(unifrnd(0.5,2,1,1));
shearingCoefX = double(unifrnd(0.5,2,1,1));
shearingCoefY = double(unifrnd(0.5,2,1,1));
translationCoefX = double(int8(unifrnd(-I1r/2,I1r/2,1,1)));
translationCoefY = double(int8(unifrnd(-I1c/2,I1c/2,1,1)));
%The initialization of the first phai
phai(1,:)=[round(rotationAngle*10^n)/10^n, round(scalingCoefX*10^n)/10^n, round(scalingCoefY*10^n)/10^n, round(shearingCoefX*10^n)/10^n, round(shearingCoefY*10^n)/10^n, round(translationCoefX*10^n)/10^n, round(translationCoefY*10^n)/10^n]';
%Make the randomly generated particles from the initial prior gaussian distribution
for i = 1:M
phai(i,:) = phai(1,:) + sqrt(2) * randn; %2: the variance of the initial esimate
w(i,:)=1/M;
end
% Normalize the weights:
w = w./sum(w);
%%% Step2: Resample process
for k=1:N
for i=1:M
% rand: u (Uniform random value between 0 and 1
j=find((cumsum(w) >= max(w)),1,'first');
phai_select(i,:)=phai(j,:);
phai(i,:)=phai_select(i,:)+(sqrt(Vk^2)*randn);
I2_new=targettransform(I2,phai(i,:)); %user-defined function to apply the generated transformation to the target image
E_I1=entropy(I1);
I=E_I1+entropy(I2_new)-joint_entropy(I1,I2_new); %joint_entropy: user defined function to calculate joint entropy of the two images
w(i)=(1/sqrt(2*pi*w_v))*exp(-((E_I1-I)^2)/(2*w_v));
% w(i)=prod(prod(((1/sqrt(2*pi*w_v))*exp(-((I2_new-I1)^2)/(2*w_v)))));
end
% Normalize the weights
w = w./sum(w);
% Reduce the noise standard deviation
Vk=beta*Vk;
phai_est=mean(phai);
end

compute S transform and its square value in matlab

let us consider following code taken from
http://www.mathworks.com/matlabcentral/fileexchange/45848-stockwell-transform--s-transform-/content/stran.m
to compute S transform, here is my code
function ST=stran(h)
% Compute S-Transform without for loops
%%% Coded by Kalyan S. Dash %%%
%%% IIT Bhubaneswar, India %%%
[~,N]=size(h); % h is a 1xN one-dimensional series
nhaf=fix(N/2);
odvn=1;
if nhaf*2==N;
odvn=0;
end
f=[0:nhaf -nhaf+1-odvn:-1]/N;
Hft=fft(h);
%Compute all frequency domain Gaussians as one matrix
invfk=[1./f(2:nhaf+1)]';
W=2*pi*repmat(f,nhaf,1).*repmat(invfk,1,N);
G=exp((-W.^2)/2); %Gaussian in freq domain
% End of frequency domain Gaussian computation
% Compute Toeplitz matrix with the shifted fft(h)
HW=toeplitz(Hft(1:nhaf+1)',Hft);
% Exclude the first row, corresponding to zero frequency
HW=[HW(2:nhaf+1,:)];
% Compute Stockwell Transform
ST=ifft(HW.*G,[],2); %Compute voice
%Add the zero freq row
st0=mean(h)*ones(1,N);
ST=[st0;ST];
end
and consider following chirp signal
>> t = 0:0.001:2;
x = chirp(t,100,1,200,'quadratic');
i need confirmation that i am doing correctly following things
>> ST=stran(x);
>> plot(abs(ST))
?
picture is here
Posting my comment as answer:
I don't have much idea of the s-transform, but AFAIK the result of it is a 3D signal (as you can clearly see in the size of ST), so you may want to do imagesc(abs(ST)) or surf(abs(ST),'linestyle','none') instead of plot.
In your figure you have plotted 1000 lines, that's why its so chaotic.
Using
imagesc(abs(ST))

Chebyshev IIR FIlter: Got Coefficients, what next?

Here is my Matlab/Octave program
clc;
close all;
%BPF of pass 400-600Hz
fs1=300;
fp1=400;
fp2=600;
fs2=700;
samp=1500;
ap=1; %passband ripple
as=60; %stopband attenuation
%Normalizing the frequency
wp=[fp1 fp2]/(samp);
ws=[fs1 fs2]/(samp);
[N,wn]=cheb1ord_test(wp,ws,ap,as); %Generates order and cutoff parameters
[b,a]=cheby1(N,ap,wn); %Generates poles and zeros for the given order and cutoff
printf("b coeffs = %f\n",b);
printf("a coeffs = %f\n",a);
[H,W]=freqz(b,a,256);
plot(W/(2*pi),20*log10(abs(H))) %Transfer function works correctly, so coefficients are correct
%100 samples of 500hz
n = 1:100;
x=10*cos(2*pi*n*500*(1/samp));
printf("Order %d\n",N); %Depends on required ripple attenuation
figure;
subplot (2,1,1); plot(x);
y=filter(b,a,x); %**Apparently i suspect this does not work**
subplot (2,1,2); plot(y);
When i see the magnitude/frequency response, the graph is perfect and indicates 400 and 600 to be my filter cutoffs.
But however when i apply an input signal of 500Hz, i must expect to see the signal pass through the filter unharmed (as observed when i used Butterworth function), but the output is distorted and almost contains no signal
So i infer that my mistake is using the filter function to combine the chebyshev coefficients with the input signal.
If this is the problem, then how do i apply chebyshev coefficients to an input digital signal ?
For cheb1ord and cheby1 the frequencies are normalized between 0 and 1 with 1 corresponding to half the sampling frequency. You should get your wp and ws using
wp=[fp1 fp2]/(samp/2);
ws=[fs1 fs2]/(samp/2);
where samp is your sampling frequency.
I think the problem is with your x signal: I don't quite know what it is, but I can tell you what it is not and that's a 500Hz input signal.
I would define the time vector first and then apply the cos function (I assume you are sampling at 1500Hz):
f_input = 500; %Hz
t = 0:1/samp:1; % Time vector [0,1] sampled at 1500Hz
x = 10*cos(2*pi*t/f_input); % 500Hz input signal

Use Butterworth filter and recur function to filter Audio File.

I'm trying to filter a short audio file using a Butter worth filter and then a function to recursively solve the difference equation using the coefficients given by Butter.m.
My code is as follows.
[x,Fs]=wavread('bugsbunny1');
wn=2500/(Fs/2);
n=10;
[B,A]=butter(n,wn);
C=zeros(1,10);
for n=2:10
C(n-1)=-A(n); %rearrange A for the recur function
end
A=C;
x=x'; % make x a row vector
n=11:length(x);
y0=zeros(1,11);
x0=zeros(1,11);
y1=recur(A,B,n,x,x0,y0); %calculate approximation recursively
y1=[y0 y1]; %add initial conditions to vector
doing this results in y1 being a matrix of invalid data given by 'NaN', any help is appreciated thanks.
Code for the recur function:
function [ y ] = recur(a,b,n,x,x0,y0)
%Use Recursion to approximate solution to first order differential equation
% Detailed explanation goes here
N=length(a);
M=length(b)-1;
y=[y0 zeros(1,length(n))];
x=[x0 x];
a1=a(length(a):-1:1);
b1=b(length(b):-1:1);
for i=N+1:N+length(n),
y(i)=-a1*y(i-N:i-1)'+b1*x(i-N:i-N+M)';
end
y=y(N+1:N+length(n));