Plots and FFT of Rows or Columns of Data

  • #1
Ephant
148
2
Hi, the following plots the waveforms and the FFT of a signal (one channel only):
Matlab:
signal = data;   %assuming data was loaded first
Fs = 4800; % Sampling frequency
T = 1/Fs; % Sampling period
L = size(signal, 1); % Length of signal
t = (0:L-1)*T; % Time vector
X =  double(signal);
plot(1000*t,X);
title('signal');
xlabel('t (milliseconds)');
ylabel('X(t)');
X = X - mean(X);
Y = fft(X);

P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

f = Fs/L*(0:(L/2));
plot(f,P1,'LineWidth',0.5);
title('Single-Sided Amplitude Spectrum of X(t)');
xlabel('f (Hz)');
ylabel('|P1(f)|');

It plots the column of one channel. In the following, how do you make the above plot channel 1 only of the following which seems to be in rows?

Matlab:
Channels=17;
FileName='test.bin';
fid=fopen(FileName,'rb');
data=fread(fid,[Channels inf],'float32');
fclose(fid);
<Moderator's note: post edited to add CODE tags.>
 
Physics news on Phys.org
  • #2
Ephant said:
It plots the column of one channel. In the following, how do you make the above plot channel 1 only of the following which seems to be in rows?

Matlab:
Channels=17;
FileName='test.bin';
fid=fopen(FileName,'rb');
data=fread(fid,[Channels inf],'float32');
fclose(fid);
<Moderator's note: post edited to add CODE tags.>
The fread there means that the first data point in test.bin is the first data point of channel 1, the second data point the first data point of channel 2, and so on. Is this correct?

Then amend the fist line of the code to
Matlab:
signal = data(1,:);
 
  • #3
DrClaude said:
The fread there means that the first data point in test.bin is the first data point of channel 1, the second data point the first data point of channel 2, and so on. Is this correct?

Then amend the fist line of the code to
Matlab:
signal = data(1,:);

I'm not sure. The output figure (both plot and FFT, I ran them separately) is showing empty plot. The test.bin is the output from an amplifier. The following is from the documentation: What other possibilities are there besides data(1,:)?

data storage.JPG
 
  • #4
DrClaude said:
The fread there means that the first data point in test.bin is the first data point of channel 1, the second data point the first data point of channel 2, and so on. Is this correct?

Then amend the fist line of the code to
Matlab:
signal = data(1,:);

When the data storage code above was executed, the following results:


data variable.JPG



with your signal = data(1,:); it becomes:

signal variable to data.JPG



But my codes in original message reads from top to botttom. Not left to right. How do you modify my code to read from left to right.. or how do you convert the above to top to bottom.. using the code data(:,1); won't solve it either because it just reads the 1 character of each channel like this:

top to bottom.JPG
 
  • #5
Oh, i found the command. It's R=signal'; the ' will convert horizontal to vertical. Thanks for the signal = data(1,:); clue.
 
  • #6
I am using the g.USBamp, a $17500 Research grade amplifier used by major R&D centers and companies worldwide. I have some questions about the output and FFT displayed.

I compared the FFT outputs between the g.USBamp Demo software and the BCI2000 software.

In the g.USBamp software, the output is in BIN which I asked in original message and able to read the data already. I shorted the differential inputs of channel 1 (In+, In-, ground). I set sampling to 4800Hz, 0.5Hz High Pass and 1000Hz Low Pass using 8 order software Butterworth filter and 58-62Hz Notch.

gusbamp white noise.JPG


I got the following FFT output.

testwhpoint5hzhp.JPG



Why do the lower frequencies shift up? Do you call it baseline shift? (but DC "baseline shift" is supposed to be a phenomenon of time domain plot). I used the following Matlab code to run it:


Code:
Channels=17;
FileName='testwnpoint5hzhp.bin';
fid=fopen(FileName,'rb');
data=fread(fid,[Channels inf],'float32');
fclose(fid);

P  = data(1,:);
signal=P';

signal=signal-mean(signal);
Fs = 4800; % Sampling frequency
T = 1/Fs; % Sampling period
L = size(signal, 1); % Length of signal
t = (0:L-1)*T; % Time vector
X =  double(signal);
plot(t,X);
title('signal');
xlabel('t (seconds)');
ylabel('X(t)');

Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

f = Fs/L*(0:(L/2));
plot(f,P1,'LineWidth',0.5);
title('Single-Sided Amplitude Spectrum of X(t)');
xlabel('f (Hz)');
ylabel('|P1(f)|');

I compared it to BCI2000 software where I also set the filter to 4800 Hz, 0.5Hz High Pass, 1000Hz Low Pass. 8th order Butterworth. Channel 1 3 inputs shorted. Used 58-62Hz Notch. All same setting as the one used in the g.USBamp. Even at same time period of testing. And I got the following output:


bci2000 point5 Hz white noise input shorted.JPG


Why is there no shift of any kind in the BCI2000 output? Instead there is a 958Hz peak that is not seen using the g.USBamp software? What is the cause of the strange 958Hz peak? Which FFT is the correct one (the one from g.USBamp software shown in the first figure or the BCI2000 software)? The Matlab code I used for the BCI2000 is:

Code:
[ signal, states, parameters, total_samples, file_samples ] = load_bcidat ('testS001R80.dat');

Fs = 4800; % Sampling frequency
T = 1/Fs; % Sampling period
L = size(signal, 1); % Length of signal
t = (0:L-1)*T; % Time vector
X =  double(signal);
plot(1000*t,X);
title('signal');
xlabel('t (milliseconds)');
ylabel('X(t)');
X = X - mean(X);
Y = fft(X);

P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);

f = Fs/L*(0:(L/2));
plot(f,P1,'LineWidth',0.5);
title('Single-Sided Amplitude Spectrum of X(t)');
xlabel('f (Hz)');
ylabel('|P1(f)|');
 
  • #7
It's hard to say without seeing the data. If you comment out the line
Matlab:
signal=signal-mean(signal);
is the result better?
 
  • #8
DrClaude said:
It's hard to say without seeing the data. If you comment out the line
Matlab:
signal=signal-mean(signal);
is the result better?

Without the line, the result is exactly the same. Here is the exact data file I used at google drive:



Is the Matlab code correct? Pls modify it so the plotting will be accurate if something is wrong. I just used the standard FFT plot code example. Since it is g.USBamp original software used, then the binary saved file data is more accurate than the one at BCI2000 which could be numerical artifacts of the processing producing the 958Hz peak, isn't it?

In the BCI2000 FFT plot in my last message. The following is the zoomed of it. There is no gap whatsoever in the noise floor. What kind of processing can make the noise floor without any gap but compensate it by making a 958Hz peak instead? Again this is the BCI2000 output zoomed.

zoom lower fft.JPG
 
  • #9
The first data points of signal look like
1715096660021.png

so it is no wonder that you get the Fourier transform you have plotted. Doesn't look like something that would give you a peak close to 1000 Hz.
 
  • Like
Likes Ephant

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
166
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
150
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
13K
Back
Top