Plots and FFT of Rows or Columns of Data

  • Context: MATLAB 
  • Thread starter Thread starter Ephant
  • Start date Start date
  • Tags Tags
    Columns Fft
Click For Summary

Discussion Overview

The discussion revolves around plotting waveforms and performing Fast Fourier Transform (FFT) on data from a multi-channel signal, specifically focusing on how to extract and visualize data from a binary file containing multiple channels. Participants explore the implications of data organization in rows versus columns and the effects on FFT results.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • Participants discuss how to correctly extract channel data from a binary file where data is organized in rows, suggesting that the first data point corresponds to channel 1.
  • One participant proposes modifying the code to use signal = data(1,:); to select the first channel, but expresses uncertainty about the output being an empty plot.
  • Another participant questions how to modify the code to read data from left to right, indicating confusion about the data orientation.
  • A later reply suggests using the transpose operator R=signal'; to convert horizontal data to vertical.
  • One participant describes their experience with a specific amplifier and compares FFT outputs from different software, raising questions about discrepancies in frequency peaks and baseline shifts.
  • Participants inquire about the accuracy of their MATLAB code and whether the FFT results are affected by data processing methods used in different software.
  • There is a suggestion to comment out the mean subtraction line in the code to see if it improves the results, but one participant reports no change in the output.
  • Another participant notes that the initial data points do not seem to support the expected frequency peaks, questioning the validity of the FFT results.

Areas of Agreement / Disagreement

Participants express uncertainty regarding the correct method for data extraction and the interpretation of FFT results. Multiple competing views exist about the implications of data organization and processing methods, and the discussion remains unresolved.

Contextual Notes

Participants mention specific settings and configurations for the amplifier and filtering processes, but there are unresolved questions about the effects of these settings on the FFT outputs. The discussion includes references to specific data files and MATLAB code, but no consensus is reached on the correctness of the approaches taken.

Who May Find This Useful

This discussion may be useful for individuals working with multi-channel signal processing, particularly those using MATLAB for data visualization and FFT analysis, as well as those interested in the implications of data organization on analysis outcomes.

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.>
 
Physics news on Phys.org
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,:)?

data storage.JPG
 
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
 
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.

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)|');
 
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.

zoom lower fft.JPG
 
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   Reactions: Ephant

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 16 ·
Replies
16
Views
15K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K