MATLAB regress function and Normalizing Data - matlab

I am trying to perform a multiple linear regression in MATLAB using the regress function, and I am using a number of different variables that involve different scales and units. I am assume the answer to this question is yes, but should I normalize each variable before running the regression? I'm not sure if MATLAB does so automatically. Thanks for the help!

Yes, you should. If you want to normalize it between 0 and 1, you could use mat2gray function (assuming "vector" as your list of variables).
norm_vect = mat2gray(vector);
This function is used to convert matrix into an image, but works well if you don't want to write yours. You also can use a simple normalization like:
for i = 1:length(vector)
norm_vect(i) = (vector(i)-min(vector))/(max(vector)-min(vector));
end

Related

Adaptive sampling in matlab

Suppose I have a function which is extremely time consuming to evaluate and I want to generate an interpolated version of it using as few function evaluation as possible. Is there a built in function in Matlab to do that (something like FunctionInterpolation from Mathematica) ?
The procedure is not very difficult and I am aware of freely available implementations (in other languages) like http://scipy-central.org/item/53/1/adaptive-sampling-of-1d-functions but considering that matlab has build in triangular mesh refinement, I think there might be also something like this to be used in one dimension.
You may use fplot with two output arguments, as
[X,Y] = fplot(fun,limits,...)
described in
http://www.mathworks.fr/fr/help/matlab/ref/fplot.html
for instance
fun = #(x) 1./(1+x.^2)
[X,Y] = fplot(fun,[-10, 10])

Matlab dwt across specified dimension

I have a dataset Sig of size 65536 x 192 in Matlab. If I want to take the one-dimensional fft along the second dimension, I could either do a for loop:
%pre-allocate ect..
for i=1:65536
F(i,:) = fft(Sig(i,:));
end
or I could specify the dimension and do it without the for loop:
F = fft(Sig,[],2);
which is about 20 times faster for my dataset.
I have looked for something similar for the discrete wavelet transform (dwt), but been unable to find it. So I was wondering if anyone knows a way to do dwt across a specified dimension in Matlab? Or do I have to use for loops?
In your loop FFT example, it seems you operate on lines. Matlab use a Column-major order. It may explain the difference of performance. Is the performance the same if you operate on columns ?
If this is the right explanation, you could use dwt in a loop.
A solution if you really need performance is to do your own MEX calling a C discrete wavelet transform library the way you want.
I presume you're using the function from the Wavelet Toolbox: http://www.mathworks.co.uk/help/toolbox/wavelet/ref/dwt.html
The documentation doesn't seem to describe acting on an array, so it's probably not supported. If it does allow you to input an array, then it will operate on the first non-singleton dimension or it will ignore the shape and treat it as a vector.

Cross-correlation in matlab without using the inbuilt function?

can someone tell how to do the cross-correlation of two speech signals (each of 40,000 samples) in MATLAB without using the built-in function xcorr and the correlation coefficient?
Thanks in advance.
You can do cross-correlations using fft. The cross-correlation of two vectors is simply the product of their respective Fourier transforms, with one of the transforms conjugated.
Example:
a=rand(5,1);
b=rand(5,1);
corrLength=length(a)+length(b)-1;
c=fftshift(ifft(fft(a,corrLength).*conj(fft(b,corrLength))));
Compare results:
c =
0.3311
0.5992
1.1320
1.5853
1.5848
1.1745
0.8500
0.4727
0.0915
>> xcorr(a,b)
ans =
0.3311
0.5992
1.1320
1.5853
1.5848
1.1745
0.8500
0.4727
0.0915
If there some good reason why you can't use the inbuilt, you can use a convolution instead. Cross-correlation is simply a convolution without the reversing, so to 'undo' the reversing of the correlation integral you can first apply an additional reverse to one of your signals (which will cancel out in the convolution).
Well yoda gave a good answer but I thought I mention this anyway just in case. Coming back to the definition of the discrete cross correlation you can compute it without using (too much) builtin Matlab functions (which should be what Matlab do with xcorr). Of course there is still room for improvment as I did not try to vectorize this:
n=1000;
x1=rand(n,1);
x2=rand(n,1);
xc=zeros(2*n-1,1);
for i=1:2*n-1
if(i>n)
j1=1;
k1=2*n-i;
j2=i-n+1;
k2=n;
else
j1=n-i+1;
k1=n;
j2=1;
k2=i;
end
xc(i)=sum(conj(x1(j1:k1)).*x2(j2:k2));
end
xc=flipud(xc);
Which match the result of the xcorr function.
UPDATE: forgot to mention that in my opinion Matlab is not the appropriate tool for doing real time cross correlation of large data sets, I would rather try it in C or other compiled languages.

How to draw the transfer function of an RC circuit using MATLAB?

If I have an RC circuit with transfer function 1/(1+sRC) how do I draw the transfer function using MATLAB?
Num2=[1];
Den2=[R*C 1];
RCcirc=tf(Num2,Den2);
How do I declare the R and the C so that there are no errors?
tf is the wrong tool for plotting the transfer function. Try these instead:
Use linspace to generate a range of values for s. Give R and C reasonable values of your choice.
Read up on arithmetic operations in MATLAB, especially ./
Look at how to use plot and familiarize yourself with the command using some simple examples from the docs.
With these you should be able to plot the transfer function in MATLAB :)
First of all you need to understand what transfer function you want. Without defined values of R and C you won't get any transfer function. Compare it to this, you want to plot a sine wave: x = sin(w*t), I hope you can agree with me that you cannot plot such a function (including axes) unless I specifically say e.g. t is the time, ranging from 0 seconds to 10 seconds and w is a pulsation of 1 rad/s. It's exactly the same with your RC network: without any values, it is impossible for numerical software such as MATLAB to come up with a plot.
If you fill in those values, you can use th tf function to display the transfer function in whatever way you like (e.g. a bode plot).
On the other hand, if you just want the expression 1/(1+s*R*C), take a look at the symbolic toolbox, you can do such things there. But to make a plot, you will still have to fill in the R and C value (and even a value for your Laplace variable in this case).

matlab - two variables least squares function approximation

I have a function of two variables of the type: y = f(x1,x2) to be approximated and I would like to use least squares method to do it.
Polyval and Polyfit work with two-dimensional function, here I need to solve a three-dimensional function.
Thanks in advance.
G.B.
I've solved it in this way
A = [x1.^2,x1.*x2,x2.^2,x1,x2,ones(length(x1),1)];
c=A\y;
yEval = c(1)*x1.^2+c(2)*x1.*x2+c(3)*x2.^2+c(4)*x1+c(5)*x2+c(6);
Thanks anyway for your help.
Regards,
G.B.
Looking at your function, it looks like you're using a full quadratic response surface to fit against.
You could use x2fx function to generate all the terms. It's nothing groundbreaking here, but it might be a little cleaner. You could also use it to do not just OLS fitting, but also use the robust methods.
Here's some code I've written:
% set up terms for the variables, linear, quadratic, interactive, and constant
paramVEcomponents= x2fx([MAPkpa,RPM],'quadratic');
% robust fit using a Talwar weighting function
[coefs,robuststats]= robustfit(paramVEcomponents(2:6),(CAM2.*TEMPd./MAPkpa),'talwar');
% generating points for all the data we have based on the new parameters of the response surface
GMVEhat= paramVEcomponents * coefs;