Error in Cross correlation (crosscorr command) in MATLAB
I have a problem with a code. I have two kinds of data (lets call them a and b). These are cells, and in each cell I have double/numbers (see images attached). I would like to cross correlate , using a loop, data of a with data of b. I am using the following code:
for i=1:numel(a)
[c,lag]=crosscorr(a{i},b{i})
r = [0.65, 1];
ii = c >= r(1) & c <= r(2)
% this finds the index of he rows(2) that have x in between
idx = find(abs(c) > r(1) & abs(c) <= r(2));
% number of intervals with positive check
numIdx{i} = sum(abs(c) > r(1) & abs(c) <= r(2))
Final{i}=(numIdx{i})'
n=Final'
end
but I realise that the results are wrong, as I should have 100% correlation bacause of the similarity of the numbers.
1 answer

crosscorr will compute the correlation between shifted versions of your data. This is useful to synchronize time series or to align versions of an image. You will have only one of the attempts that matches perfectly1, the rest may be uncorrelated. To measure correlation between aligned samples you can use corr
See also questions close to this topic

How do I detect a M1 mac processor in MATLAB?
Is there a way of detecting a M1 mac in MATLAB? MATLAB has
ismac
but that presumably won't differentiate between processor types. 
can the exogenous inputs for NARX Neural Network Model be an annual data and the target variable be a daily data?
My final year project involves predicting covid19 daily cases while considering exogenous factors (population, Gross Domestic Product (GDP), Median Age, Diabetes Prevalence, Human Development Index (HDI), Air Pollution as Particulate matter level (PM2.5)).
I was learning about the NARX Neural Network model, but mostly the researches, tutorial videos I have come through have used exogenous inputs that are dynamic or have a time series element. But, for my research I have planned to use static exogenous inputs which are for the year 2019.
This means that I have an annual data by country for these exogenous input data where each country will have only one value for the input. On the other hand, my target data which is covid19 cases is a daily data. Therefore, I am facing difficulty on how to implement the NARX model for such case.
I managed to find a similar research that has done this but the research didn't really explain how the model is implemented. Therefore, I would like to know whether can exogenous inputs for NARX Neural Network Model be an annual data and the target be a daily data? and can it be implemented in matlab R2021a using GUI or the ntstool?

How to read a single image from a folder and then classify it with a trained neural network?
I trained my network for skin tumor classification in MATLAB and then saved it. Now I want to test the accuracy of some new images. It works fine using imageDatastore but what I really want is to be able to browse in my computer for an image and classify it using the trained network. Is this possible? Below I have attached the test code for a set of images.
load incercare.mat imds= imageDatastore('E:/LICENTA/BD_Skin_Cancer_Screening/mini_data/test/', ... 'IncludeSubfolders', true, ... 'LabelSource', 'foldernames' ); [YPred,probs] = classify(netTransfer,imds); accuracy = mean(YPred == imds.Labels) for i=1:10 subplot(2,5,i) I = readimage(imds,idx(i)); imshow(I) label = YPred(idx(i)); title(string(label) + ", " + num2str(100*max(probs(idx(i),:)),3) + "%"); end

How to use test input file in Xcode C++ by using command line arguments?
I am trying to use sample.in.txt as a test input in Xcode C++. Thus, I go to edit schema and add arguments like this.
However, when I run the project, it is still getting the input from the user and do nothing about the input file I typed on the arguments. I print the argv, then it all prints correctly.
This is my main function
int main(int argc, char** argv) { int T; cin >> T; for(int i = 0; i < T; i++){ int size, shift; cin >> size >> shift; Array test(size); for(int j = 0; j < size; j++){ int x; cin >> x; test.add(j, x); } test.shift(shift); } return 0; }

why do i have ping command eror in discord.py?
so i have problem with discord.py commands , i want to make ping command which will response with bot ping, i have code like this but it doesn't work, can someone help my?
@bot.command() async def ping(ctx): await ctx.send(f"Pong :ping_pong:! Bot latency: **{round(bot.latency * 1000)}ms**"
i tryed to write code like this:
@client.command() async def ping(ctx): await ctx.send(f"Pong :ping_pong:! Bot latency: **{round(bot.latency * 1000)}ms**"
but it still don't works

Discord.py AFK Command Rewrite
Here is my afk command:
@client.event async def on_message(message): if message.mentions results = collection.find({"member": message.author.id}) for result in results: collection.delete_one(result) if message.content == result: await message.channel.send(f"This person is currently AFK. \nFor: {reason}") await client.process_commands(message)
Error:
File "main.py", line 124 if message.mentions ^ SyntaxError: invalid syntax
I was a little confused on why. any ideas?

Trying to do cross correlation in C
I am doing a project which is locating a sound source. I am using three microphones to capture data and then do cross correlation to find the time difference. The time difference is then used to locate the sound source. The issue I think am having is with the cross correlation of my code. I am doing this in C and from my understanding FFT needs to be done and so I am using the FFTW library to do this. The result of my cross correlation is always 0. The microphones are capturing data by sampling them, and then after a loud sound it captures data for half a buffer and then writes the data to a text file. I am using a circular buffer for storing the data. After the trigger event occurs, the data is reordered and then fast fourier transformed. Then multiplied and then inverse transformed. I then determine when the peak occurs from the output of the IFFT. Most of the time it is 0 but I am not sure why. So I wrote the data into a text file where I then imported the data into MATLAB. This was to test the xcorr function with my data. The results of this match my cross correlation code in C. So I am now not sure of where the issue lies with my code.
I have added a link to the data I acquired in a text file.1 Then for my cross correlation in C I have the following code.
#include <string.h> #include <stdlib.h> #include <stdint.h> #include <math.h> #include <complex.h> /* Standard Library of Complex Numbers */ #include <fftw3.h> int i,size = 8; //defining the size of the buffer and FFT int peak_detec(double); void reverse(double a[]); int peak_detec(double a) // function which calculates the maximum value of an array { int b= 0; double Largest; int Position; Largest = a; for(b=1; b<10; b++) { if(Largest<a) { Largest=a; Position = b; } } return Position; } void reverse(double a[]) // function which reverses the order of the array send to the function { int i; double temp[size]; for(i = 1; i < size+1; i++) { temp[i1] = a[sizei]; } for (i=0; i< size;i++) { a[i]=temp[i]; } } int main(void) { double array[] = {0.7,0.1,0.1,0.1,0.1,0.1,0.1,0.1}; //my two arrays for FFT double array2[] = {0.1,0.1,0.1,0.1,0.1,0.1,0.7,0.1}; double *out,*out2,*out3; //pointer for the output double *err,*err2; //pointer for errors double *double_array; double *double_array2; //****pointer for complex arrays****// double complex *out_cpx; double complex *out_cpx2; double complex *out_cpx3; //****defining all the plans for fft****// fftw_plan fft; fftw_plan ifft; fftw_plan fft2; fftw_plan ifft2; fftw_plan ifft3; //****allocating memory****// out_cpx = (double complex*) fftw_malloc(sizeof(double complex)*(2*size)); out = (double *) malloc((2*size)*sizeof(double)); err = (double *) malloc((2*size)*sizeof(double)); out_cpx2 = (double complex*) fftw_malloc(sizeof(double complex)*(2*size)); out2 = (double *) malloc((2*size)*sizeof(double)); err2 = (double *) malloc((2*size)*sizeof(double)); out_cpx3 = (double complex*) fftw_malloc(sizeof(double complex)*(2*size)); out3 = (double *) malloc((2*size)*sizeof(double)); double_array = (double*) malloc(sizeof(double)*(2*size)); double_array2 = (double*) malloc(sizeof(double)*(2*size)); //****setting up fftw and ifft plans****// fft = fftw_plan_dft_r2c_1d((2*size), double_array, out_cpx, FFTW_ESTIMATE); //Setup fftw plan for fft ifft = fftw_plan_dft_c2r_1d((2*size), out_cpx, out, FFTW_ESTIMATE); //Setup fftw plan for ifft fft2 = fftw_plan_dft_r2c_1d((2*size), double_array2, out_cpx2, FFTW_ESTIMATE); //Setup fftw plan for fft ifft2 = fftw_plan_dft_c2r_1d((2*size), out_cpx2, out2, FFTW_ESTIMATE); //Setup fftw plan for ifft ifft3 = fftw_plan_dft_c2r_1d((2*size), out_cpx3, out3, FFTW_ESTIMATE); //Setup fftw plan for ifft //reverse(array2); memcpy (double_array, array, sizeof(double)*(size)); memset (double_array + size+1, 0, sizeof(double) * (size1)); memcpy (double_array2, array2, sizeof(double)*(size)); memset (double_array2 + size+1, 0, sizeof(double) * (size1)); //****executing fft****// fftw_execute(fft); fftw_execute(fft2); double complex scale = 1.0/(2 * size); for (i=0;i<2*size;i++) //loop which multiplies the FFT arrays { out_cpx3[i] = out_cpx[i]*out_cpx2[i]*scale; } //****executing ifft****// fftw_execute(ifft); fftw_execute(ifft2); fftw_execute(ifft3); //****for loop which finds the largest value of the array****// int Position =0; double Largest; Largest = out3[0]; for(i=1; i<2*size; i++) { if(Largest<=out3[i]) { Largest=out3[i]; Position = i ; } } printf("Position of peak value: %d\n",Position); //****frees the memory****// fftw_destroy_plan(fft); fftw_destroy_plan(fft2); fftw_destroy_plan(ifft); fftw_destroy_plan(ifft2); fftw_destroy_plan(ifft3); fftw_free(out_cpx); fftw_free(out_cpx2); fftw_free(out_cpx3); free(double_array); free(double_array2); free(err); free(out); free(err2); free(out2); free(out3); return 0; }
Can anyone help with my issue? I am trying to do cross correlation and with the data in the shared link but does not seem to work. It is my first time asking a question on here, I think I have given enough information but if not then I can try provide more if something does not make sense.

cross correlation of more than two files in python
I have a large numerical data and I must split it into 100 files and then compute
C(i,j) = 〈x(ti)x(tj)〉
. To this end, I must do the averaging on the 100 data sets and make a matrix and plot it as a density plot.So what I have done is:
import numpy as np data = np.loadtxt('data1.txt') np.split(data, 100) lines_per_file = 10485 smallfile = None small_filename_num = 1 with open("data1.txt", "r") as bigfile: for lineno, line in enumerate(bigfile): if lineno % lines_per_file == 0: if smallfile: smallfile.close() smallfile = open('File%d.txt' % small_filename_num, "w") small_filename_num += 1 smallfile.write(line) if smallfile: smallfile.close()
Now I want to calculate the cross correlation but I don't know how.

windowed crosscorrelation using numpy
I have two data series, that are slightly shifted to each other. Both contain nan values, that need to be respected. Hence I would like to align them automatically. My idea is to use crosscorrelation and numpy arrays to solve the problem. The code below is extremely slow and I would like to speed things up, but as a non python expert, I don't see any possibilities for improvement.
The idea is to have a baseline and target array. The code calculates the offset of each target position relative to the baseline in a windowed fashion. For each window it is calculated how much the data point has to be shifted for an optimal alignment. The first point that can be aligned is at
window_size//2
and the last atbasline.sizewindow_size//2
window_size = 50 N = 100 randN = 10 baseline = np.random.rand(N,) target = np.random.rand(N,) mask=np.zeros(N,dtype=bool) mask[:randN] = True np.random.shuffle(mask) baseline[mask] = np.nan np.random.shuffle(mask) target[mask] = np.nan stacked = np.column_stack((baseline,target)) stacked_windows = sliding_window_view(stacked, (window_size,2)) offset_np = np.zeros([stacked.shape[0], ]) offset_np[:] = np.nan for idx in range(stacked_windows.shape[0]): window = stacked_windows[idx] baseline_window_np = window.reshape(window_size,2)[:,0] target_window_np = window.reshape(window_size,2)[:,1] # baseline_window_masked = ma.masked_invalid(baseline_window_np) target_window_masked = ma.masked_invalid(target_window_np) # cc_np = np.empty([window_size + 1, ], dtype=np.float32) cc_np = np.zeros([window_size, ]) cc_np[:] = np.nan for lag in range(int(window_size//2),int(window_size//2)): masked_tmp = ma.masked_invalid(shift_numpy(target_window_masked, lag)) cc_np[lag+int(window_size//2)] = ma.corrcoef(baseline_window_masked,masked_tmp)[0,1] if not np.isnan(cc_np).all(): offset_np[window_size//2+idx] = np.floor(window_size//2)np.argmax(cc_np) result_np = np.column_stack((stacked, offset_np)) result_df = df = pd.DataFrame(result_np, columns = ['baseline','target','offset'])