Plots and FFT of Rows or Columns of Data

  • MATLAB
  • Thread starter Ephant
  • Start date
  • Tags
    Columns Fft
In summary, the document discusses the process of plotting and applying the Fast Fourier Transform (FFT) to rows or columns of data. It explains how to visualize the frequency components of data sets, highlighting the significance of FFT in analyzing periodic signals. The text includes practical steps for implementing these techniques, emphasizing the importance of data organization and interpretation of results in understanding underlying patterns in the data.
  • #1
Ephant
147
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

FAQ: Plots and FFT of Rows or Columns of Data

What is an FFT and how is it used in data analysis?

FFT, or Fast Fourier Transform, is an algorithm that computes the discrete Fourier transform (DFT) and its inverse. It is widely used in data analysis to transform time-domain data into the frequency domain, allowing scientists to identify periodic patterns, noise, and other frequency-related characteristics in the data. By applying FFT to rows or columns of data, one can analyze the frequency components present in the dataset.

How do I plot the results of an FFT?

To plot the results of an FFT, you typically take the output of the FFT, which consists of complex numbers representing amplitude and phase information. You can compute the magnitude of these complex numbers to get the amplitude spectrum. Using a plotting library (such as Matplotlib in Python), you can create a plot of the frequency versus the magnitude. Make sure to properly label the axes and choose an appropriate range of frequencies for your data.

Can I apply FFT to both rows and columns of a 2D dataset?

Yes, you can apply FFT to both rows and columns of a 2D dataset. Applying FFT to rows will give you the frequency components along the horizontal direction, while applying it to columns will provide the frequency components along the vertical direction. This can be particularly useful in image processing or analyzing spatial data, where you may want to examine frequency characteristics in both dimensions.

What are some common issues when performing FFT on data?

Common issues when performing FFT on data include aliasing, which occurs when the sampling rate is too low to capture the frequency content, and windowing effects, which can introduce artifacts in the frequency spectrum. Additionally, the presence of noise in the data can affect the accuracy of the FFT results. Properly pre-processing the data, such as applying a window function and ensuring adequate sampling rates, can help mitigate these issues.

How can I interpret the output of an FFT?

The output of an FFT typically consists of complex numbers, which can be converted to magnitudes and phases. The magnitudes represent the strength of various frequency components, while the phases indicate their timing. When interpreting the output, focus on the peaks in the magnitude spectrum, as these correspond to dominant frequencies in the data. Comparing the frequency components against the expected behavior of the system being analyzed can provide insights into its characteristics and dynamics.

Similar threads

Replies
5
Views
1K
Replies
1
Views
2K
Replies
9
Views
4K
Replies
16
Views
14K
Replies
4
Views
3K
Replies
1
Views
2K
Back
Top