CUDA fft 1d different results from MATLAB fft - matlab

I want to use GPU to speed up my matlab program but I find out a problem.
The fft result is different from CUDA to matlab.
I have tried many time but can't solve it.
So I come here for help.
The original data: name:cj1;size:1*8
And in matlab use the code:
a1=fft(cj1)';
Get the result:
the fft result of matlab
And cuda code:
cuFloatComplex *idata_m;
idata_m = (cuFloatComplex*)malloc(M * sizeof(cuFloatComplex));
for (int i = 0; i < 8; i++)
{
idata_m[i].x = initA[i];
idata_m[i].y = initB[i];
}
cuComplex *dev_test;
cudaMalloc((void**)&dev_test, M * sizeof(cuFloatComplex));
cudaMemcpy(dev_test, idata_m, M * sizeof(cuFloatComplex), cudaMemcpyHostToDevice);
cufftHandle plantest;
cufftPlan1d(&plantest, 8, CUFFT_C2C, 1);
cufftExecC2C(plantest, dev_test, dev_test, CUFFT_FORWARD);//forward
cuComplex *test_out;
test_out = (cuFloatComplex*)malloc( M * sizeof(cuFloatComplex));
cudaMemcpy(test_out, dev_test, 8 * sizeof(cuFloatComplex), cudaMemcpyDeviceToHost);
the input data is the same to the original data in matlab
the fft result of cuda
the inserest thing is these two result are very similar but in the wrong order.
So what can I do to make the result the same to the result of matlab?

The imaginary part of the input data used with the CUDA code is the negative of that used with Matlab. So you really are computing the FFT of the complex conjugated input, which inverts the order of the result. To obtain the same results with CUDA you should be using the same input.
Also of note, in Matlab, the ' operator computes the complex-conjugate transpose, so you probably want to compare your CUDA results with a1=transpose(fft(cj1)); instead.

Related

How do I compute the determinant of a transfer function matrix without having to use "syms"?

I intend to compute the determinant of a transfer matrix and then subject to a nyquist analysis by making the nyquist plot but the problem is that the determinant command doesn't recognizes the transfer matrix. The code is shown below
clc
clear all;
close all;
g11 = tf(12.8,[16.7 1],'InputDelay',1)
g12 = tf(-18.9,[21 1],'InputDelay',3)
g21 = tf(6.6,[10.9 1],'InputDelay',7)
g22 = tf(-19.4,[14.4 1],'InputDelay',3)
G=[g11 g12 ; g21 g22]
[re,im,w] = nyquist(G)
F=2.55;
s=tf('s');
%syms s;
ggc11 = g11*(0.96*(1+3.25*F*s)/(3.25*F^2*s))
ggc12 = g12*(0.534*(1+3.31*F*s)/(3.31*F^2*s))
ggc21 = g21*(0.96*(1+3.25*F*s)/(3.25*F^2*s))
ggc22 = g22*(0.534*(1+3.31*F*s)/(3.31*F^2*s))
GGc=[ggc11 ggc12 ; ggc21 ggc22];
L=eye(2)+ GGc;
W= -1 + det(L)
nyquist(W)
The error that appears is as follows
Undefined function 'det' for input arguments of type 'ss'.
Error in BLT_code (line 30)
W= -1 + det(L)
I would like to avoid the 'syms' command as I would not be able to do the nyquist plot then. Is there any alternative way of computing the nyquist plot of the same ?
I am stuck in the same boat, trying to calculate the determinant of transfer function matrices for the purpose of checking the MIMO Nyquist stability criteria, see MIMO Stability ETH Zurich Lecture slides (pg 10). Unfortunately there does not seem to be a simple MATLAB command for this. I figured it can be evaluated manually.
If you have a TF matrix G(s) of the following form:
G = [g_11 g_12; g_21 g_22];
you can obtain the determinant by evaluating it as per its original definition as
det_G = g_11*g_22 - g_12*g_21;
This will result in a 1x1 TF variable. Of course, this method gets much too complicated for anything above a 2x2 system.

Matlab simulation error

I am completely new to Matlab. I am trying to simulate a Wiener and Poisson combined process.
Why do I get Subscripted assignment dimension mismatch?
I am trying to simulate
Z(t)=lambda*W^2(t)-N(t)
Where W is a wiener process and N is a poisson process.
The code I am using is below:
T=500
dt=1
K=T/dt
W(1)=0
lambda=3
t=0:dt:T
for k=1:K
r=randn
W(k+1)=W(k)+sqrt(dt)*r
N=poissrnd(lambda*dt,1,k)
Z(k)=lambda*W.^2-N
end
plot(t,Z)
It is true that some indexing is missing, but I think you would benefit from rewriting your code in a more 'Matlab way'. The following code is using the fact that Matlab basic variables are matrices, and compute the results in a vectorized way. Try to understand this kind of writing, as this is the way to exploit Matlab more efficiently, along with writing shorter and readable code:
T = 500;
dt = 1;
K = T/dt;
lambda = 3;
t = 1:dt:T;
sqdtr = sqrt(dt)*randn(K-1,1); % define sqrt(dt)*r as a vector
N = poissrnd(lambda*dt,K,1); % define N as a vector
W = cumsum([0; sqdtr],1); % cumulative sum instead of the loop
Z = lambda*W.^2-N; % summing the processes element-wiesly
plot(t,Z)
Example for a result:
you forget index
Z(k)=lambda*W.^2-N
it must be
Z(k)=lambda*W(k).^2-N(k)

Why do the principal component values from Scipy and MATLAB not agree?

I was training to do some PCA reconstroctions of MNIST on python and compare them to my (old) reconstruction in maltab and I happened to discover that my reconstruction don't agree. After some debugging I decided to print a unique characteristic of the principal components of each one to reveal if they were the same and I discovered to my surprised that they were not the same. I printing the sum of all components and I got different numbers. I did the following in matlab:
[coeff, ~, ~, ~, ~, mu] = pca(X_train);
U = coeff(:,1:K)
U_fingerprint = sum(U(:))
%print 31.0244
and in python/scipy:
pca = pca.fit(X_train)
U = pca.components_
print 'U_fingerprint', np.sum(U)
# prints 12.814
why are the twi PCA's not computing the same value?
All my attempts and solving this issue:
The way I discovered this was because when I was reconstructing my MNIST images, the python reconstructions where much much closer to their original images by a lot. I got error of 0.0221556788645 in python while in MATLAB I got errors of size 29.07578. To figure out where the difference was coming from I decided to finger print the data sets (maybe they were normalized differently). So I got two independent copies the MNIST data set (that were normalized by dividing my 255) and got the finger prints (summing all numbers in data set):
print np.sum(x_train) # from keras
print np.sum(X_train)+np.sum(X_cv) # from TensorFlow
6.14628e+06
6146269.1585420668
which are (essentially) same (one copy from tensorflow MNIST and the other from Keras MNIST, note MNIST train data set has about 1000 less training set so you need to append the missing ones). To my surprise, my MATLAB data had the same finger print:
data_fingerprint = sum(X_train(:))
% prints data_fingerprint = 6.1463e+06
meaning the data sets are exactly the same. Good, so the normalization data is not the issue.
In my MATLAB script I am actually computing the reconstruction manually as follow:
U = coeff(:,1:K)
X_tilde_train = (U * U' * X_train);
train_error_PCA = (1/N_train)*norm( X_tilde_train - X_train ,'fro')^2
%train_error_PCA = 29.0759
so I thought that might be the problem because I was using the interface python gave for computing the reconstructions as in:
pca = PCA(n_components=k)
pca = pca.fit(X_train)
X_pca = pca.transform(X_train) # M_train x K
#print 'X_pca' , X_pca.shape
X_reconstruct = pca.inverse_transform(X_pca)
print 'tensorflow error: ',(1.0/X_train.shape[0])*LA.norm(X_reconstruct_tf - X_train)
print 'keras error: ',(1.0/x_train.shape[0])*LA.norm(X_reconstruct_keras - x_train)
#tensorflow error: 0.0221556788645
#keras error: 0.0212030354818
which results in different error values 0.022 vs 29.07, shocking difference!
Thus, I decided to code that exact reconstruction formula in my python script:
pca = PCA(n_components=k)
pca = pca.fit(X_train)
U = pca.components_
print 'U_fingerprint', np.sum(U)
X_my_reconstruct = np.dot( U.T , np.dot(U, X_train.T) )
print 'U error: ',(1.0/X_train.shape[0])*LA.norm(X_reconstruct_tf - X_train)
# U error: 0.0221556788645
to my surprise, it has the same error as my MNIST error computing by using the interface. Thus, concluding that I don't have the misconception of PCA that I thought I had.
All that lead to me to check what the principal components actually where and to my surprise scipy and MATLAB have different fingerprint for their PCA values.
Does anyone know why or whats going on?
As warren suggested, the pca components (eigenvectors) might have different sign. After doing a finger print by adding all components in magnitude only I discovered they have the same finger print:
[coeff, ~, ~, ~, ~, mu] = pca(X_train);
K=12;
U = coeff(:,1:K)
U_fingerprint = sumabs(U(:))
% U_fingerprint = 190.8430
and for python:
k=12
pca = PCA(n_components=k)
pca = pca.fit(X_train)
print 'U_fingerprint', np.sum(np.absolute(U))
# U_fingerprint 190.843
which means the difference must be because of the different sign of the (pca) U vector. Which I find very surprising, I thought that should make a big difference, I didn't even consider it making a big difference. I guess I was wrong?
I don't know if this is the problem, but it certainly could be. Principal component vectors are like eigenvectors: if you multiply the vector by -1, it is still a valid PCA vector. Some of the vectors computed by matlab might have a different sign than those computed in python. That will result in very different sums.
For example, the matlab documentation has this example:
coeff = pca(ingredients)
coeff =
-0.0678 -0.6460 0.5673 0.5062
-0.6785 -0.0200 -0.5440 0.4933
0.0290 0.7553 0.4036 0.5156
0.7309 -0.1085 -0.4684 0.4844
I have my own python PCA code, and with the same input as in matlab, it produces this coefficient array:
[[ 0.0678 0.646 -0.5673 0.5062]
[ 0.6785 0.02 0.544 0.4933]
[-0.029 -0.7553 -0.4036 0.5156]
[-0.7309 0.1085 0.4684 0.4844]]
So, instead of simply summing the coefficient array, try summing the absolute values of the coefficients. Alternatively, ensure that all the vectors have the same sign convention before summing. You could do that by, say, multiplying each column by the sign of the first element in that column (assuming none of them are zero).

FFT and IFFT - difference between results in Matlab and openCV

I'm trying to convert this simple Matlab code to C++ with openCV:
localstd=sqrt(abs(ifft2(fft2(output).*gf)));
It means taking the fft of the matrix "output", multiplying it element by element with the matrix "gf", then taking the ifft of that and then taking the magnitude of that.
I'm trying the following simple code:
Mat complexI;
dft(output, complexI,cv::DFT_SCALE||DFT_COMPLEX_OUTPUT);
Mat copmlexI2=Mat(n,n,CV_32F);
mulSpectrums(complexI,gf,copmlexI2,DFT_COMPLEX_OUTPUT);
dft(copmlexI2,copmlexI2,cv::DFT_INVERSE||DFT_COMPLEX_OUTPUT);
Mat planes[]= {Mat::zeros(output.size(), CV_32F), Mat::zeros(output.size(), CV_32F)};;
split(copmlexI2, planes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude
Mat localstd = planes[0];
for (int i=0;i<localstd.rows;i++){
for (int j=0;j<localstd.cols;j++){
localstd.at<float>(i,j)= sqrt(localstd.at<float>(i,j));
}
}
for (int i=0;i<localstd.rows;i++){
for (int j=0;j<localstd.cols;j++){
localstd.at<float>(i,j)/= 255;
}
}
It's very simple. I'm taking the dft of "output", multiply it's spectrum with "df" and take the ifft of that. Next, I split the result into the real and imaginary plane and take the magnitude. Finally, I take the sqrt of that and normalize by dividing with 255.
The results I get are very different than what I get in Matlab. What am I missing here? Any ideas on how to fix the code?
Thanks in advance!
This is not correct
cv::DFT_INVERSE||DFT_COMPLEX_OUTPUT
, if you want combine binary values you should use "binary or":
cv::DFT_INVERSE | DFT_COMPLEX_OUTPUT
or + operation
cv::DFT_INVERSE + DFT_COMPLEX_OUTPUT
A || B - is logical or. A, B and result may be only true or false.
A | B - is bitwise or.
You can also try DFT_SCALE flag.
DFT_SCALE scales the result: divide it by the number of array
elements. Normally, it is combined with DFT_INVERSE.

Matlab firpm fails for large AFR data arrays

Here is a quick & dirty code for trying to create a high precision equalizer:
bandPoints = 355;
for n = 1:bandPoints
x = (n / (bandPoints + 2));
f = (x*x)*(22000-20)+20; % 20...22000
freqs(n) = f;
niqfreqs(n) = f/22050.0;
amps(n) = 0;
end
amps(bandPoints+1) = 0; % firpm needs even numbers
niqfreqs(bandPoints+1) = 1; % firpm needs even numbers
% set some point to have a high amplitude
amps(200) = 1;
fircfs = firpm(101,niqfreqs,amps);
[h,w] = freqz(fircfs,1,512);
plot(w/pi,abs(h));
legend('firpm Design')
but it gives me
Warning:
*** FAILURE TO CONVERGE ***
Probable cause is machine rounding error.
and all FIR coefficients are 0.
If I lower the n parameter from 101 to 91, firpm works without errors, but the frequency response is far from desired. And taking into account, that I want to calculate FIR coefficients for a hardware DSP FIR module, which supports up to 12288 taps, how can I make Matlab calculate the needed coefficients? Is firpm capable of doing this or do I need to use another approach (inverse FFT) in both Matlab and later in my application C++ code?
Oh, it seems MP algorithm really cannot handle this, so I need some other solution:
http://www.eetimes.com/design/embedded/4212775/Designing-very-high-order-FIR-filters-with-zero-stuffing
I guess, I'll have to stick with inverse FFT then.