Plot FFT for one channel when data is in rows

  • Topic: MATLAB 
  • Thread starter Ephant
  • Start date
  • Tags Tags
    Columns Fft
8 replies · 5K views · last reply
Ephant
Messages
147
Reaction score
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.>
 
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,:);
 
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,:)?

Text slide titled Data storage with MATLAB code: Channels=17; FileName='test.bin'; fid=fopen(FileName,'rb'); data=fread(fid,[Channels inf],'float32'); fclose(fid);
 
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:


MATLAB Variables window showing a 17x261504 double array named data, with rows 1 to 17 and columns 1 to 20 displayed; the first 16 rows hold numeric values and row 17 is zeros.



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

MATLAB Variables window showing a 1x261504 double array named signal, displayed as a single row of values from columns 1 to 20, with the Workspace panel beside it.



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:

MATLAB Variables window showing a 17x1 double variable named signal as a column of 17 values, with the first entries -1.03, 5.717, -1.46 and the last entry 0.
 
Oh, i found the command. It's R=signal'; the ' will convert horizontal to vertical. Thanks for the signal = data(1,:); clue.
 
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.

Screenshot of the g.USBamp demo software showing a noisy time-domain trace of one channel, with the title UB-2011.04.09 and a sample axis from 0 to 4800.


I got the following FFT output.

MATLAB plot titled Single-Sided Amplitude Spectrum of X(t): |P1(f)| against f (Hz) from 0 to 2500, peaking near 800 Hz, with a spike near 58.58 Hz.



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:


MATLAB figure of the single-sided amplitude spectrum of X(t), with f (Hz) from 0 to 2500 on the x-axis and |P1(f)| from 0 to 1.2 on the y-axis. One tall peak near 958.5 Hz reaches about 1.0, with a data tip reading X: 958.5, Y: 0.7509, and small values elsewhere.


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)|');
 
It's hard to say without seeing the data. If you comment out the line
Matlab:
signal=signal-mean(signal);
is the result better?
 
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.

MATLAB figure titled Single-Sided Amplitude Spectrum of X(t), plotting |P1(f)| against f (Hz) from 0 to 2500 Hz, with dense spectral peaks up to about 1500 Hz and a broad noise floor near 0.005 to 0.015.
 
The first data points of signal look like
Line plot of a signal over indices 0 to 100, with a multiplier of 10^4 on the y-axis. Large oscillations near the start peak near 16000 and dip near -8000, then decay to near zero by about index 20.

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.