matlab generalized cross correlation and ROTH Filter - matlab

I have a problem for finding cross correlation of tho signals. No filtere version works but ROTH filter version does not work. Can you tell me the problem please ? Thanks
tho=2;
A=1;
t=-15:0.1:15;
FFTLength=length(t);
x= rect( t+1,tho,A ); % First rectangle signal
plot(t,x)
hold on
y=rect(t-4,tho,A);
plot(t,y,'r')
for i=1:length(x)
x(i)=x(i)+normrnd(0,0.1);
end
figure
plot(t,x,'r')
for i=1:length(y)
y(i)=y(i)+normrnd(0,0.1);
end
figure
plot(t,y,'r')
Rxx = xcorr(x);
Ryy = xcorr(y);
Rxy = xcorr(x,y);
Sxx = fft(Rxx,FFTLength);
Syy = fft(Ryy,FFTLength);
Sxy = fft(Rxy,FFTLength);
X=fft(x,301);
Y=fft(y,301);
%Filtering
H=1;
X2=X.*H;
Y2=Y.*H;
x_t=ifft(X2);
y_t=ifft(Y2);
[correlation,lags] = xcorr(y_t,x_t);
delay = lags(find(correlation==max(correlation)))
disp('Plain time ')
X2=fft(x).*(1./Sxx);
Y2=fft(Y).*(1./Sxx);
x2=real(ifft(X2));
y2=real(ifft(Y2));
[correlation2,lags2] = xcorr(x2,y2);
delay2 = lags2(find(correlation2==max(correlation2)))
disp('ROTH Filter ');
by the way rect is ;
function [ y ] = rect( t,tho,A )
for i = 1 : length(t)
if -(tho / 2) <= t(i) && t(i) <= (tho / 2)
y(i) = A * 1;
else
y(i) = 0;
end
end
end

Related

Plotting animation results in blank graph

I have written this code and it will not plot. I run it with a 0:.5:100 and get a blank graph in Matlab. As well this is a inclined plane friction problem. I need to create an animation in Matlab that shows the blocks sliding based on inputs.
%Numerical Project Code 1
%mass ratio input
mratio = input('Enter the Mass Ratio(m/M): ');
%angle input
theta = input('Enter Angle in Degrees Between 0 and 90: ');
%static coeff input
mus = input('Enter Coefficient of Static Friction: ');
%kinetic coeff input
muk = input('Enter Coefficient of Kinetic Friction: ');
%constants
g = 9.81;
%interface formating
disp('--------------------------------------------');
disp('All Friction Forces are given in terms of the mass on the slope (M)');
%Loops
%NETUP
if mratio > sind(theta)
%static only
if mratio <= (sind(theta) + (mus*cosd(theta)))
ff = g*(mratio - sind(theta));
fprintf("Friction Force = %f M Newtons\n",ff);
fprintf("The Direction of the Friction Force is down the slope and the block not moving.\n")
%kinetic only
else
ff = muk * g * cosd(theta);
fprintf("Friction Force = %f M Newtons\n",ff);
fprintf("The Direction of the Friction Force is down the slope and the block is sliding up the slope.\n");
end
%NETDOWN
elseif mratio < sind(theta)
%static only
if sind(theta) <= (mratio + (mus*cosd(theta)))
ff = g*(sind(theta) - mratio);
fprintf("Friciton Force = %f M Newtons\n",ff);
fprintf("The Direction of the Friction Force is up the slope and the block is not moving.\n")
%kinetic only
else
ff = muk * g * cosd(theta);
fprintf("Friction Force = %f M Newtons\n",ff);
fprintf("The Direction of the Friction Force is up the slope and the block is sliding down the slope.\n");
end
%NETZERO
else
fprintf("Friction Force = 0 Newtons\n");
end
%graph
for i = 0:0.01:1
mratiog = i;
if mratiog > sind(theta)
if mratiog <= (sind(theta) + (mus*cosd(theta)))
ffg = g*(mratiog - sind(theta));
else
ffg = muk * g * cosd(theta);
end
elseif mratiog < sind(theta)
if sind(theta) <= (mratiog + (mus*cosd(theta)))
ffg = g*(sind(theta) - mratiog);
else
ffg = muk * g * cosd(theta);
end
else
ffg = 0;
end
plot (ffg,mratio, 'r:')
end
In addition to Erik's comments I see two other issues:
the plot changes its axis with each new iteration, so you can not see
the animation properly. It would work better if you calculate all
your points before plotting them. Then you can use min and max of data
dimensions to set axis limits. If you don't use 'hold on' you need to set the limits in each iteration.
you need to use 'pause' to slow down the
animation. otherwise you see only the last data point.
Here is a working example of the 'graph' part:
%graph
i_arr = 0:0.01:1;
n = numel(i_arr);
ffg_mratio_arr = zeros(n, 2);
for i = 1:n
mratiog = i_arr(i);
if mratiog > sind(theta)
if mratiog <= (sind(theta) + (mus*cosd(theta)))
ffg = g*(mratiog - sind(theta));
else
ffg = muk * g * cosd(theta);
end
elseif mratiog < sind(theta)
if sind(theta) <= (mratiog + (mus*cosd(theta)))
ffg = g*(sind(theta) - mratiog);
else
ffg = muk * g * cosd(theta);
end
else
ffg = 0;
end
ffg_mratio_arr(i,:) = [ffg mratio];
end
% calculating the axis limits
x_min = min(ffg_mratio_arr(:, 1))-1;
x_max = max(ffg_mratio_arr(:, 1))+1;
y_min = min(ffg_mratio_arr(:, 2))-1;
y_max = max(ffg_mratio_arr(:, 2))+1;
% animation
figure;
for i = 1:n
plot (ffg_mratio_arr(i,1), ffg_mratio_arr(i,2), 'ro');
xlim([x_min x_max]);
ylim([y_min y_max]);
pause(0.01);
end

MATLAB 3D lines are invisible

Hello I'am suffered from a problem
As you can see I want a draw 3D graph.
Problem is when I draw sphere lines are invisible.
Here is simple version of my source
clear all; close all; clc
n=1;
n_inner_drone=3;
n_outter_drone=2;
length=100;
initial_d = zeros(1,n);
inner_x = zeros(n_inner_drone,n);
inner_y = zeros(n_inner_drone,n);
inner_z = zeros(n_inner_drone,n);
outter_x = zeros(n_outter_drone,n);
outter_y = zeros(n_outter_drone,n);
outter_z = zeros(n_outter_drone,n);
radius= length;
disp('test');
%%%%%%%%%%%%%%%%%%%%%% Sphere
% figure()
% [x, y, z] = sphere;
% h = surfl(x*length, y*length, z*length);
% hSurf = surf(X,Y,Z,'EdgeColor','none','LineStyle','none','FaceLighting','phong');
% set(h, 'FaceAlpha', 0.05)
% surf(x*length, y*length, z*length,
% shading interp
hold on
%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:n_inner_drone
k=1;
while 1
x_temp= randi([-length, length], 1, 1);
y_temp= randi([-length, length], 1, 1);
z_temp= randi([-length, length], 1, 1);
dist = sqrt(x_temp^2 + y_temp^2 + z_temp^2);
if dist<radius
if i==1
initial_d(k) = dist;
end
inner_x(i,k) = x_temp;
inner_y(i,k) = y_temp;
inner_z(i,k) = z_temp;
k = k+1;
end
if k == n+1, break, end
end
end
ideal_direction_length = ones(1,n);
ideal_direction_length = ideal_direction_length * length;
ideal_direction_length = ideal_direction_length - initial_d;
k=1;
random_x = inner_x(1,:);
random_y = inner_y(1,:);
random_z = inner_z(1,:);
random_moving_distance = zeros(1,n);
moving_distance = 0;
trigger = 0;
while 1
if trigger == 0
direction = randi([1, 6], 1, 1);
trigger = 1;
end
if direction == 1
random_x(k) = random_x(k) + 1;
elseif direction == 2
random_x(k) = random_x(k) - 1;
elseif direction == 3
random_y(k) = random_y(k) + 1;
elseif direction == 4
random_y(k) = random_y(k) - 1;
elseif direction == 5
random_z(k) = random_z(k) + 1;
elseif direction == 6
random_z(k) = random_z(k) - 1;
end
dist = sqrt(random_x(k)^2 + random_y(k)^2 + random_z(k)^2);
moving_distance = moving_distance+1;
%%%%%%%%%% Line
plot3(random_x(n),random_y(n),random_z(n),'k+')
%%%%%%%%%%%%%%%
if dist>radius
random_moving_distance(k) = moving_distance;
k = k+1;
moving_distance = 0;
trigger = 0;
end
if k == n+1, break, end
end
plot3(inner_x(1,n),inner_y(1,n),inner_z(1,n),'r*')
for k=2:n_inner_drone
plot3(inner_x(k,n),inner_y(k,n),inner_z(k,n),'b*')
end
for k=1:n_outter_drone
plot3(outter_x(k,n),outter_y(k,n),outter_z(k,n),'k*')
end
At the first, I suspected I worngly draw lines, but without sphere I can see lines as fig2.
Those anyone who knows about this problem.
Please answer to me and I will very appericiate about it.
Thanks for reading.
I think it is because:
plot3(gravity_x(n),gravity_y(n),gravity_z(n))
is not a line. Its a single point.
plot3(gravity_x(n:n+1),gravity_y(n:n+1),gravity_z(n:n+1))
is a line.

Poisson PDE solver on a disked shaped domain with finite difference method using matlab

For my studies I had to write a PDE solver for the Poisson equation on a disc shaped domain using the finite difference method.
I already passed the Lab exercise. There is one issue in my code I couldn't fix. Function fun1 with the boundary value problem gun2 is somehow oscillating at the boundary. When I use fun2 everything seems fine...
Both functions use at the boundary gun2. What is the problem?
function z = fun1(x,y)
r = sqrt(x.^2+y.^2);
z = zeros(size(x));
if( r < 0.25)
z = -10^8*exp(1./(r.^2-1/16));
end
end
function z = fun2(x,y)
z = 100*sin(2*pi*x).*sin(2*pi*y);
end
function z = gun2(x,y)
z = x.^2+y.^2;
end
function [u,A] = poisson2(funame,guname,M)
if nargin < 3
M = 50;
end
%Mesh Grid Generation
h = 2/(M + 1);
x = -1:h:1;
y = -1:h:1;
[X,Y] = meshgrid(x,y);
CI = ((X.^2 +Y.^2) < 1);
%Boundary Elements
Sum= zeros(size(CI));
%Sum over the neighbours
for i = -1:1
Sum = Sum + circshift(CI,[i,0]) + circshift(CI,[0,i]) ;
end
%if sum of neighbours larger 3 -> inner note!
CI = (Sum > 3);
%else boundary
CB = (Sum < 3 & Sum ~= 0);
Sum= zeros(size(CI));
%Sum over the boundary neighbour nodes....
for i = -1:1
Sum = Sum + circshift(CB,[i,0]) + circshift(CB,[0,i]);
end
%If the sum is equal 2 -> Diagonal boundary
CB = CB + (Sum == 2 & CB == 0 & CI == 0);
%Converting X Y to polar coordinates
Phi = atan(Y./X);
%Converting Phi R back to cartesian coordinates, only at the boundarys
for j = 1:M+2
for i = 1:M+2
if (CB(i,j)~=0)
if j > (M+2)/2
sig = 1;
else
sig = -1;
end
X(i,j) = sig*1*cos(Phi(i,j));
Y(i,j) = sig*1*sin(Phi(i,j));
end
end
end
%Numberize the internal notes u1,u2,......,un
CI = CI.*reshape(cumsum(CI(:)),size(CI));
%Number of internal notes
Ni = nnz(CI);
f = zeros(Ni,1);
k = 1;
A = spalloc(Ni,Ni,5*Ni);
%Create matix A!
for j=2:M+1
for i =2:M+1
if(CI(i,j) ~= 0)
hN = h;hS = h; hW = h; hE = h;
f(k) = fun(X(i,j),Y(i,j));
if(CB(i+1,j) ~= 0)
hN = abs(1-sqrt(X(i,j)^2+Y(i,j)^2));
f(k) = f(k) + gun(X(i,j),Y(i+1,j))*2/(hN^2+hN*h);
A(k,CI(i-1,j)) = -2/(h^2+h*hN);
else
if(CB(i-1,j) ~= 0) %in negative y is a boundry
hS = abs(1-sqrt(X(i,j)^2+Y(i,j)^2));
f(k) = f(k) + gun(X(i,j),Y(i-1,j))*2/(hS^2+h*hS);
A(k,CI(i+1,j)) = -2/(h^2+h*hS);
else
A(k,CI(i-1,j)) = -1/h^2;
A(k,CI(i+1,j)) = -1/h^2;
end
end
if(CB(i,j+1) ~= 0)
hE = abs(1-sqrt(X(i,j)^2+Y(i,j)^2));
f(k) = f(k) + gun(X(i,j+1),Y(i,j))*2/(hE^2+hE*h);
A(k,CI(i,j-1)) = -2/(h^2+h*hE);
else
if(CB(i,j-1) ~= 0)
hW = abs(1-sqrt(X(i,j)^2+Y(i,j)^2));
f(k) = f(k) + gun(X(i,j-1),Y(i,j))*2/(hW^2+h*hW);
A(k,CI(i,j+1)) = -2/(h^2+h*hW);
else
A(k,CI(i,j-1)) = -1/h^2;
A(k,CI(i,j+1)) = -1/h^2;
end
end
A(k,k) = (2/(hE*hW)+2/(hN*hS));
k = k + 1;
end
end
end
%Solve linear system
u = A\f;
U = zeros(M+2,M+2);
p = 1;
%re-arange u
for j = 1:M+2
for i = 1:M+2
if ( CI(i,j) ~= 0)
U(i,j) = u(p);
p = p+1;
else
if ( CB(i,j) ~= 0)
U(i,j) = gun(X(i,j),Y(i,j));
else
U(i,j) = NaN;
end
end
end
end
surf(X,Y,U);
end
I'm keeping this answer short for now, but may extend when the question contains more info.
My first guess is that what you are seeing is just numerical errors. Looking at the scales of the two graphs, the peaks in the first graph are relatively small compared to the signal in the second graph. Maybe there is a similar issue in the second that is just not visible because the signal is much bigger. You could try to increase the number of nodes and observe what happens with the result.
You should always expect to see numerical errors in such simulations. It's only a matter of trying to get their magnitude as small as possible (or as small as needed).

Undefined variable in 'if' statement

I'm writing a script for an aerodynamics class and I'm getting the following error:
Undefined function or variable 'dCt_dx'.
Error in Project2_Iteration (line 81)
Ct = trapz(x,dCt_dx)
I'm not sure what the cause is. It's something to do with my if statement. My script is below:
clear all
clc
global dr a n Vinf Vr w rho k x c cl dr B R beta t
%Environmental Parameters
n = 2400; %rpm
Vinf = 154; %KTAS
rho = 0.07647 * (.7429/.9450); %from mattingly for 8kft
a = 1084; %speed of sound, ft/s, 8000 ft
n = n/60; %convert to rps
w = 2*pi*n;
Vinf = (Vinf*6076.12)/3600; %convert from KTAS to ft/s
k = length(c);
dr = R/k; %length of each blade element
for i = 1:k
r(i) = i*dr - (.5*dr); %radius at center of blade element
dA = 2*pi*r*dr; %Planform area of blade element
x(i) = r(i)/R;
if x(i) > .15 && x(i-1) < .15
i_15 = i;
end
if x(i) > .75 && x(i-1) < .75
i_75h = i;
i_75l = i-1;
end
Vr(i) = w*r(i) + Vinf;
%Aerodynamic Parameters
M = Vr(i)/a;
if M > 0.9
M = 0.9;
end
m0 = 0.9*(2*pi/(1-M^2)^0.5); %lift-curve slope (2pi/rad)
%1: Calculate phi
phi = atan(Vinf/(2*pi*n*r(i)));
%2: Choose Vo
Vo = .00175*Vinf;
%3: Calculate Theta
theta = atan((Vinf + Vo)/(2*pi*n*r(i)))-phi;
%4:
if option == 1
%calculate cl(i) from c(i)
sigma = (B*c(i))/(pi*R);
if sigma > 0
cl(i) = (8*x(i)*theta*cos(phi)*tan(phi+theta))/sigma;
else
cl(i) = 0;
end
else %option == 2
%calculate c(i) from cl(i)
if cl(i) ~= 0
sigma = (8*x(i)*theta*cos(phi)*tan(phi+theta))/cl(i);
else
sigma = 0;
end
c(i) = (sigma*pi*R)/B;
if c(i) < 0
c(i) = 0;
end
end
%5: Calculate cd
cd(i) = 0.0090 + 0.0055*(cl(i)-0.1)^2;
%6: calculate alpha
alpha = cl(i)/m0;
%7: calculate beta
beta(i) = phi + alpha + theta;
%8: calculate dCt/dx and dCq/dx
phi0 = phi+theta;
lambda_t = (1/(cos(phi)^2))*(cl(i)*cos(phi0) - cd(i)*sin(phi0));
lambda_q = (1/(cos(phi)^2))*(cl(i)*sin(phi0) + cd(i)*cos(phi0));
if x(i) >= 0.15
dCt_dx(i) = ((pi^3)*(x(i)^2)*sigma*lambda_t)/8; %Roskam eq. 7.47, pg. 280
dCq_dx(i) = ((pi^3)*(x(i)^3)*sigma*lambda_q)/16; %Roskam eq. 7.48, pg 280
else
dCt_dx(i) = 0;
dCq_dx(i) = 0;
end
%calculate Mdd
t(i) = (0.04/(x(i)^1.2))*c(i);
Mdd(i) = 0.94 - (t(i)/c(i)) - cl(i)/10;
end
%9: calculate Ct, Cq, Cd
Ct = trapz(x,dCt_dx)
Cq = trapz(x,dCq_dx)
D = 2*R;
Q=(rho*(n^2)*(D^5)*Cq)
T=(rho*(n^2)*(D^4)*Ct)
When I step through your script, I see that the the entire for i = 1:k loop is skipped because k=0. You set k = length(c), but c was never initialized to a value, so it has length zero.
Because of this, dCt_dx is never given a value--and more importantly the majority of your script is never run.
If you're going to be using MATLAB in the future, I really suggest learning how to do this. It makes it a lot easier to find bugs. Try looking at this video.

Matlab: How to fix Least Mean square algorithm code

I am studying about Least Mean Square algorithm and saw this code. Based on the algorithm steps, the calculation of the the error and weight updates looks alright. However, it fails to give the correct output. Can somebody please help in fixing the problem? The code has been taken from:
http://www.mathworks.com/matlabcentral/fileexchange/35670-lms-algorithm-implementation/content/lms.m
clc
close all
clear all
N=input('length of sequence N = ');
t=[0:N-1];
w0=0.001; phi=0.1;
d=sin(2*pi*[1:N]*w0+phi);
x=d+randn(1,N)*0.5;
w=zeros(1,N);
mu=input('mu = ');
for i=1:N
e(i) = d(i) - w(i)' * x(i);
w(i+1) = w(i) + mu * e(i) * x(i);
end
for i=1:N
yd(i) = sum(w(i)' * x(i));
end
subplot(221),plot(t,d),ylabel('Desired Signal'),
subplot(222),plot(t,x),ylabel('Input Signal+Noise'),
subplot(223),plot(t,e),ylabel('Error'),
subplot(224),plot(t,yd),ylabel('Adaptive Desired output')
EDIT
The code from the answer :
N = 200;
M = 5;
w=zeros(M,N);
mu=0.2;%input('mu = ');
y(1) = 0.0;
y(2) = 0.0;
for j = 3:N
y(j) = 0.95*y(j-1) - 0.195*y(j-2);
end
x = y+randn(1,N)*0.5;
%x= y;
d = y;
for i=(M+1):N
e(i) = d(i) - x((i-(M)+1):i)*w(:,i);
w(:,i+1) = w(:,i) + mu * e(i) * x((i-(M)+1):i)';
end
for i=(M+1):N
yd(i) = x((i-(M)+1):i)*w(:,i);
end
The weight matrix w which stores the coefficients are all zero, meaning that the LMS equations are not working correctly.
I also do not find any mistake in your code. But I doubt that this algorithm is suitable for this kind of noise. You will get better results when using a filter of higher order (M in this case):
M = 5;
w=zeros(M,N);
mu=0.2;%input('mu = ');
for i=(M+1):N
e(i) = d(i) - x((i-(M)+1):i)*w(:,i);
w(:,i+1) = w(:,i) + mu * e(i) * x((i-(M)+1):i)';
end
for i=(M+1):N
yd(i) = x((i-(M)+1):i)*w(:,i);
end
N=input('length of sequence N = ');
t=[0:N-1];
w0=0.001; phi=0.1;
d=sin(2*pi*[1:N]*w0+phi);
x=d+randn(1,N)*0.5;
w=zeros(1,N);
mu=input('mu = ');
for i=1:N
yd(i)=w*x';
e(i) = d(i) - w * x';
for m=1:N
w(m) = w(m) + mu * e(i) * x(m);
end
end
subplot(221),plot(t,d),ylabel('Desired Signal'),
sub plot(222),plot(t,x),ylabel('Input Signal+Noise'),
subplot(223),plot(t,e),ylabel('Error'),
subplot(224),plot(t,yd),ylabel('Adaptive Desired output')
What you were missing was the multiplication of error term in a single iteration with each sample of input and separate update of weights