I am working on balancing an air-spindle. For the unbalance analysis I use an accelerometer (NI device). I have the voltage signal from the accelerometer corresponding to the vibration of spindle at a particular frequency (rpm) saved in an excel file. To analyze the unbalance from this vibration signal I use the "fft" function in Matlab. My data is sampled at a sampling frequency of 100,000 Hz. I am using the same example code given in the fft documentation. In the documentation the fft has peak at the frequency corresponding to the frequency of sine wave signal (50Hz and 120Hz). When I use the same code for getting fft of my sampled data for a rotational frequency of 40Hz (2400rpm), I do not get the peak at 40Hz. Am I doing something wrong?? where should I mention the rotational frequency of my spindle in the fft code. I would like to know the phase and magnitude of vibration signal for my rotational frequency (40Hz). the fft plot looks like this. My code is as follows. Any help is much appreciated.
%For vibration analysis of signal without any trial mass.
filename = '2400RPM.xlsx';
sheet = 1;
xlRange = 'C40:C516039'; % Column C has sampled vibration data
x = xlsread(filename,sheet,xlRange);
T=1/100000;
Fs=1/T;
L = length(x);
t= (0:L-1)*T;
Y = fft(x);
mag1 = abs(Y/L);
mag = mag1(1:L/2+1);
mag(2:end-1) = 2*mag(2:end-1);
ph1 = rad2deg(Y/L);
ph = ph1(1:L/2+1);
ph(2:end-1) = 2*ph(2:end-1);
f=Fs*(0:(L/2))/L;
%PLOTTING RESULTS
%--------------------------------------
subplot(2,2,[1,2])
plot(t,x);
title('Vibration Signal: 2400RPM');
xlabel('Time (seconds)');
ylabel('Amplitude (voltage)');
subplot(2,2,3)
plot(f,mag);
title('Magnituge Plot');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
subplot(2,2,4)
plot(f,ph);
title('Phase Plot');
xlabel('Frequency (Hz)');
ylabel('Phase (degree)');
Related
I have a matlab code to Frequency modulation and demodulation a signal. My code is work well for modulation part. My message signal is m and modulated signal is u, code plot the message signal and its integral in one graph for plotting 1.
Then signal modulated with carrier and program plots the modulated signal in time domain for plotting 2.
After that, by the help of some code blocks program find the frequency spectrum of modulated signal and message signal to plot graph of them for plotting 3.
In demodulation part program make some fundamental calculation for FM detection, then to obtain message signal it uses filter.
Last part program plots the graph of recovered signal with message signal to compare them.
I summarized all code because ı do not know whre is the problem.
My problem about plotting 3 when I make zoom graph 3 I see some phase foldings or like it.Graph is not symmetric according to y-axis.
I did not solve this problem, I research about them and I decided to use unwrap(). Although I tried a lot, I could not be successful. How can I get rid of this phase folding with unwrap() function. Thank you.
My matlab code is ;
ts = 0.0001;% Sampling interval
t0 = 0.15; % Duration
t = 0:ts:t0;% define time vector
%% OTHER PARAMETERS
fc = 200; % Carrier signal frequency
kf =50; % Frequency deviation constant
fs = 1/ts; % Sampling frequency
%% MESSAGE SIGNAL SIMPLY
m = 1*(t<t0/3)-2*(t<2*t0/3).*(t>=t0/3);
%% Integration of m
int_m(1) = 0;
for k =1:length(m)-1
int_m(k+1) = int_m(k) + m(k)*ts;
end
%% PLOTTING 1
figure; subplot(211); % Message signal
plot(t,m);grid on;xlabel('time');ylabel('Amplitude');
title('m(t)');ylim([-3 2]);
subplot(212);plot(t,int_m);% Integral of message signal
grid on; xlabel('time');ylabel('Amplitude');title('integral of m(t)');
ylim([-0.07 0.07]);
%% FM MODULATED SIGNAL
u = cos(2*pi*fc*t + 2*pi*kf*int_m);
%% PLOTTING 2
figure; plot(t,u); % Modulated signal in time domain
grid on;xlabel('time');
ylabel('Amplitude');title('FM :u(t)');
ylim([-1.2 1.2]);
%% FINDING FREQUENCY SPECTRUM AND PLOTTING 3
% Frequency spectrum of m(t)
f=linspace(-1/(2*ts),1/(2*ts),length(t));
M=fftshift(fft(m))./length(t); % Taking fourier transform for m(t)
U=fftshift(fft(u))./length(t); % Taking fourier transform for u(t)
figure;subplot(211); % Frequence spectrum of m(t)
plot(f,abs(M)); grid;
xlabel('Frequency in Hz');xlim([-500 500]);
ylabel('Amplitude');title('Double sided Magnitude spectrum of m(t)');
subplot(212);plot(f,abs(U)); % Frequency spectrum of u(t)
grid;xlabel('Frequency in Hz');xlim([-500 500]);
ylabel('Amplitude');title('Double sided Magnitude spectrum of u(t)');
%% DEMODULATION (Using Differentiator)
dem = diff(u);
dem = [0,dem];
rect_dem = abs(dem);
%% Filtering out High Frequencies
N = 80; % Order of Filter
Wn = 1.e-2; % Pass Band Edge Frequency.
a = fir1(N,Wn);% Return Numerator of Low Pass FIR filter
b = 1; % Denominator of Low Pass FIR Filter
rec = filter(a,b,rect_dem);
%% Finding frequency Response of Signals
fl = length(t);
fl = 2^ceil(log2(fl));
f = (-fl/2:fl/2-1)/(fl*1.e-4);
mF = fftshift(fft(m,fl)); % Frequency Response of Message Signal
fmF = fftshift(fft(u,fl)); % Frequency Response of FM Signal
rect_demF = fftshift(fft(rect_dem,fl));% Frequency Response of Rectified FM Signal
recF = fftshift(fft(rec,fl)); % Frequency Response of Recovered Message Signal
%% PLOTTING 4
figure;subplot(211);plot(t,m);grid on;
xlabel('time');ylabel('Amplitude');
title('m(t)');ylim([-3 2]);
subplot(212);plot(t,rec);
title('Recovered Signal');xlabel('{\it t} (sec)');
ylabel('m(t)');grid;
My problem is in this third graph to show well I put big picture
k = -(length(X)-1)/2:1:length(X)/2;
Your k is not symmetric.
If you work with symmetric k is it working fine?
I need to check the quality of the EEG signal, to see if there is any noise in the signal (e.g. the 50 Hz line noise).
For what I have read so far, a good way to check would be to conduct Single-sided amplitude spectrum analyses both for all electrodes and for single electrodes (e.g. Cz).
I have run the following code:
%Open the file
FileName = 'Data.dat';
fid = fopen(FileName);
if fid < 0, error('Cannot open file'); end
signal = fread(fid, Inf, 'float32');
fclose(fid);
%Set the parameters of the recording
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = length(signal); % Length of signal
%Amplitude spectrum analysis
NFFT = 2^(nextpow2(L)-1);
x=zeros(NFFT,1);
x(1:NFFT,1) = signal(1:NFFT,1);
YY = fft(x,NFFT)/L;
ff = Fs/2*linspace(0,1,NFFT/2+1);
%is taking the positive half of the spectrum (NFFT/2 +1 gives this, including 0 and nyquist, hence the +1) and mapping it onto your real frequencies from 'normalised frequency'.
% Plot single-sided amplitude spectrum.
figure; plot(ff,2*abs(YY(1:NFFT/2+1))), title('Single-Sided Amplitude Spectrum of tp'), xlabel('Frequency (Hz)'), ylabel('|Y(f)|');
I am not sure how to plot only one channel (e.g. Cz being channel 28).
(I have 64 channels, with 10-20 IS).
Any suggestion would be very much appreciated.
I'm using the pwelch method in matlab to compute the power spectra for some wind speed measurements. So, far I have written the following code as an example:
t = 10800; % number of seconds in 3 hours
t = 1:t; % generate time vector
fs = 1; % sampling frequency (seconds)
A = 2; % amplitude
P = 1000; % period (seconds), the time it takes for the signal to repeat itself
f1 = 1/P; % number of cycles per second (i.e. how often the signal repeats itself every second).
y = A*sin(2*pi*f1*t); % signal
fh = figure(1);
set(fh,'color','white','Units', 'Inches', 'Position', [0,0,6,6],...
'PaperUnits', 'Inches', 'PaperSize', [6,6]);
[pxx, f] = pwelch(y,[],[],[],fs);
loglog(f,10*(pxx),'k','linewidth',1.2);
xlabel('log10(cycles per s)');
ylabel('Spectral Density (dB Hz^{-1})');
I cannot include the plot as I do not have enough reputation points
Does this make sense? I'm struggling with the idea of having noise at the right side of the plot. The signal which was decomposed was a sine wave with no noise, where does this noise come from? Does the fact that the values on the yaxis are negative suggest that those frequencies are negligible? Also, what would be the best way to write the units on the y axis if the wind speed is measured in m/s, can this be converted to something more meaningful for environmental scientists?
Your results are fine. dB can be confusing.
A linear plot will get a good view,
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
y = sin(2 * pi * 50 * t); % 50Hz signal
An fft approach,
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
subplot(1,2,1);
plot(f,2*abs(Y(1:NFFT/2+1)))
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
pwelch approach,
subplot(1,2,2);
[pxx, freq] = pwelch(y,[],[],[],Fs);
plot(freq,10*(pxx),'k','linewidth',1.2);
xlabel('Frequency (Hz)');
ylabel('Spectral Density (Hz^{-1})');
As you can see they both have peak at 50Hz.
Using loglog for both,
So "noise" is of 1e-6 and exists in fft as well, and can be ignored.
For your second question, I don't think the axis will change it will be frequency again. For Fs you should use the sampling frequency of wind speed, like if you have 10 samples of speed in one second your Fs is 10. Higher frequencies in your graph means more changes in wind speed and lower frequencies represent less changes for the speed.
I have an audio file , which represent the sound of a motor running at 2500rpm my aim is to get the period of this signal, so I can automaticlly tell what is the motor speed is. To do that I take a part of the signal and run get it autocorrelation , hopping that this willl tell the period of the singal! but I just don't get it :
here is a part of my code :
clear;
clc;
[x0,Fs] = audioread('_2500.wav');
x= x0(1:2000,1);
xc = xcorr(x);
clf;
subplot(3,1,1);
plot(x);
subplot(3,1,2);
plot(xc);
[peaks,locs] = findpeaks(xc);
hold on
subplot(3,1,3)
plot(xc(locs),'ro');
and here are the plot :
and how should I consider the sampling frequency, which is : 44100 ?
You can use the autocorrelation or FFT of the signal to find where is the maximum:
% Parameters
Fc = 1e1;
Fs = 1e3;
% Signal
t = 0:1/Fs:1;
x = sin(2*pi*Fc*t);
% FFT
Y = abs(fft(x));
[~,I] = max(Y(1:floor(end/2)));
% Frequency and period
F = I-1;
T = 1/F;
% Plot
figure;
subplot(2,1,1); plot(t,x);
subplot(2,1,2); plot(Y);
disp(['The frequency is ',mat2str(F),'Hz, and the period is ',mat2str(T),'sec.']);
This and this post are related.
To go from your auto-correlation function xc to an estimate of the fundamental frequency, do:
fs = 44100; % Unit: Hz
xc = xc((length(xc) - 1) / 2 + 1: end); % Get the half on the positive time axis.
[~, pidx] = findpeaks(xc);
period = (pidx(1) - 1) / fs;
F0 = 1 / period; % Estimated fundamental frequency.
Note that there are other potentially more robust fundamental frequency / pitch estimation algorithms. Doing a google scholar search on "fundamental frequency estimation" or "pitch estimation" will lead you to some good reviews.
you find all peaks using "findpeaks" funciton, now compute the difference between each peak
P=diff(locs)
your period can be :
max(P)
The peiod of 250hz sine at 22050 sample rate, is about 88, the Frequency of your signal is equivalent at the Period if you do (Fs/Period) == Frequency
If you Know the frequency of your signal you can find the period just do Fs/Frequency
Suppose that I'm simulating SAR signal processing in matlab. You know a block diagram like this :
Here's what I have tried up to now.
t = 0:0.01:10;
f0 = 10^(-6);
t1 = 1;
f1 = 100;
y = chirp(t,f0,t1,f1,'linear');
%starting to generate I's
y1Modulated = y.*cos(2*pi*f0*t);
y1ModulatedFrequencyDomain = fft(y1Modulated);
As you see in the diagram the signal entering the low-pass filter is an analog one. So we should use filters in
matlab ---> signal processing toolbox ---> Analog and Digital filters ---> Analog filters
But I don't know which to use or how to obtain parameters of functions like: besselap , cheblap and so on?
There are many ways to implement what you are trying to do.
Here is one way to write the code for your block diagram:
% define some simulation parameters
fs = 80e6; % sample rate of 80 MHz
f0 = 10e6; % frequency of your complex mixer
% generate the chirp with whatever parameters you need
t = 0:1/fs:1000*1/fs;
y = chirp(t,9e6,6.25e-6,11e6);
% add a bit of noise to make the simulation more realistic
% here we make the signal-to-noise ratio approximately 40 dB
y = awgn(y,40,'measured');
% apply the complex mixing
y2 = y.*exp(j.*2.*pi.*f0.*t);
% create an example lowpass filter and filter the signal to remove images
[b,a] = butter(8,0.1);
y3 = filter(b,a,y2);
% plot the signals to see what they look like
figure(1);
plot(t,y);
grid on;
title('Received Chirp Signal (time domain)');
figure(2);
plot(linspace(-fs/2,fs/2,length(y)),20.*log10(abs(fftshift(fft(y)))));
grid on;
title('Received Chirp Signal (frequency domain)');
xlabel('frequency (Hz)');
ylabel('dB');
axis([-fs/2 fs/2 -30 40]);
figure(3); hold on;
plot(t,real(y3));
plot(t,imag(y3),'r');
grid on;
title('Baseband Chirp Signal (time domain)');
figure(4);
plot(linspace(-fs/2,fs/2,length(y3)),20.*log10(abs(fftshift(fft(y3)))));
grid on;
title('Baseband Chirp Signal (frequency domain)');
xlabel('frequency (Hz)');
ylabel('dB');
axis([-fs/2 fs/2 -30 40]);
Now, you also asked which low-pass filter design to use. This depends entirely on what you are trying to achieve, and you need to specify a filter to meet your requirements. In my example above I have used an 8th order Butterworth design. But often an FIR filter is used in order to achieve a linear phase response.