Matlab fft function - matlab

The code below is from the Matlab 2011a help about fft function.
I think there is a problem here : why do they multiply t(1:50) by Fs, and then say it's time in millisecond ? Certainly, it happens to be true in this very particular case, but change the value of Fs to, say, 2000, and it won't work anymore, obviously because of this factor of 2. Right ? Quite misleading, isn't it ? What do I miss ?
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
Clearer with this :
fs = 2000; % Sampling frequency
T = 1 / fs; % Sample time
L = 1000; % Length of signal
t2 = (0:L-1)*T; % Time vector
f = 50; % signal frequency
s2 = sin(2*pi*f*t2);
figure, plot(fs*t2(1:50),s2(1:50)); % NOT good
figure, plot(t2(1:50),s2(1:50)); % good

You don't miss anything. This is a typical bad practice of not having units as comments for each line. For example,
Fs=1000; % in [Hz]
T=1/Fs; % [sec]
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
then, the time vector has units of 1/1000 seconds, or milliseconds... (each element is 1 ms).
In the other case Fs=2000 %[Hz], which makes the time vector to have units of 1/2000 seconds...

Related

Findpeaks in Spectrum Matalb

I am trying to find peaks in spectrum but I need to extract only peak of base frequency and its harmonics, red rectangular. How to exclude anything before base frequency and only include base frequency and its 3 harmonics. I use this code but it does not help. Any idea?
pks = findpeaks(q);
findpeaks(q,'MinPeakDistance',99)
%findpeaks(q,'MinPeakHeight',0.0004)
xlim([0.1 500])
When using:
Fs = 1000;
t = 0:0.001:1-0.001; % 250-Hz sine wave modulated at 100 Hz
x = [1+cos(2*pi*100*t)].*cos(2*pi*250*t);
%envspectrum(x,Fs)
[ES,F]=envspectrum(x,Fs);
%%
findpeaks(ES,F)
% Now for only > 99 Hz (choose the freq you fancy)
idx = F >= 99; % greater than 99 Hz
findpeaks(ES(idx),F(idx)) % idx only select those F > 99
% Good? Keep the values of amplitud and location (in frequencies) of the ES
[pks,loc] = findpeaks(ES(idx),F(idx));
% If you just want to have the first 5 peaks (or the n? you choose):
% Select only 3 first.
if length(pks) > 3 % Check you didnt get less peaks
pks = pks(1:3);
loc = loc(1:3);
end
% To plot the peaks in the envelope
plot(F(idx),ES(idx),loc,pks,'r*')
I get this:
And this as result:
EDITED to adapt to new code with an example. We are applying on the envelope spectrum of a signal - [ES,F] = envspectrum(sig,Fs);-, so we know the signal and its frequency sampling (fs).
Still being the same process. You can just compute the values in findpeaks() for only those samples of your signal which are higher than 250 Hz. For that purpose, you define a logical array for the frequencies values you want to:
idx = F >= 250; % 250 Hz
And apply this logical index to the envelop spectrum of your signal and frequencies where you want to apply the function findpeaks():
[pks,loc] = findpeaks(ES(idx),F(idx));
Example:
% Let's create a signal with fs = 2500 Hz
% This is just to create an example, don't worry about these lines
fs = 2500;
f0 = 25;
n = 8;
d = 0.02;
p = 0.12;
t = 0:1/fs:1-1/fs;
z = [1 0.5 0.2 0.1 0.05]*sin(2*pi*f0*[1 2 3 4 5]'.*t)/5;
% z: our signal
% t: time array of the signal (to plot)
% How does the signal look?
plot(t,z)
% Calculating the envelope signal and its spectrum
[ES,F]=envspectrum(z,fs);
% Plot the envelop if you want:
envspectrum(z,fs)
% ES: Envelope spectrum of the signal
% F: array of frequencies used for the spectrum (half our fs for Nyquist Theorem)
% Find the peaks of the envelope spectrum
% Let's see how it is for all the envelope
findpeaks(ES,F)
% Now for only > 250 Hz (choose the freq you fancy)
idx = F >= 250; % greater than 250 Hz
findpeaks(ES(idx),F(idx)) % idx only select those F > 250
% Good? Keep the values of amplitud and location (in frequencies) of the ES
% Use 'NPeaks' input to tell function to select only 5 first peaks it can find.
[pks,loc] = findpeaks(ES(idx),F(idx), 'NPeaks', 5);
% To plot the peaks in the envelope
plot(F(idx),ES(idx),loc,pks,'r*')
% Note in this example the first peak (in 250 Hz) is not selected
% because is not a local maximum when we narrow the envelope.
% If the function select false peaks somehow, you can use
%'MinPeakHeight' to specify an absolute amplitude, 'MinPeakProminence '
%for a relative amplitude or 'Threshold' inputs for a better fit.
https://uk.mathworks.com/matlabcentral/answers/768537-use-findpeaks-on-specific-frequency-range
https://uk.mathworks.com/help/signal/ref/findpeaks.html#responsive_offcanvas

Scaling amplitudes by two for the FFT in MATLAB

I just read the example of Mablab tutorial, trying to studying the FFT function.
Can anyone tell me that for the final step, why P1(2:end-1) = 2*P1(2:end-1). In my opinion, it is not necessary to multiply by 2.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
%--------
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
%---------
X = S + 2*randn(size(t));
%---------
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
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*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
Matlab sample
The reason for the multiplication by 2 is that the spectrum returned by fft is symmetric about the DC component. Since they are showing the single-sided amplitude spectrum, the amplitude of each point is going to be doubled to account for the contributions of data on the other side of the spectrum. For example, the single-sided amplitude of pi/4 is the amplitude at pi/4 plus the amplitude at -pi/4.
The first sample is skipped since it is the DC point and therefore shared between the two sides of the spectrum.
So for example, if we look at the fft of their example signal with a 50Hz sinusoid of amplitude 0.7 and a 120Hz sinusoid of amplitude 1.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
% Compute the FFT
Y = fft(S);
% Compute the amplitudes
amplitude = abs(Y / L);
% Figure out the corresponding frequencies
f = Fs/L*[0:(L/2-1),-L/2:-1]
% Plot the result
plot(f, amplitude)
When we plot this, you'll see that it's symmetric and the original input amplitude is only realized by combining the amplitudes from both sides of the spectrum.
A slightly more explicit version of what they have done would be to be the following which sums the two halves of the spectrum
P1(2:end-1) = P1(2:end-1) + P2((L/2+2):end);
But since by definition the spectrum is symmetric, the opt to simply multiply by 2.

Random Noise Effect On Signal Filtering in Matlab

I'm woking on signal processing and Filtering. i create a noisy signal and i want to use bandpass filter to get my desire frequency.
i generate noisy signal "y" by this code :
Fs = 16000; % Sampling frequency
fNy = Fs / 2;
T = 1/Fs; % Sample time
L = 60000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz , 5.8 , 12.6 , 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t) + sin(2*pi*5.8*t) + sin(2*pi*12.6*t);
x1 = 15.3 * sin(2*pi*15.5 * t );
a = 5.2;
b = 35.5;
r = a + (b-a).*rand(1,1);
y = x + x1 + r*randn(size(t)); % Sinusoids plus noise
and i filter my signal to get 15.5 Hz signal by FIR algorithm like Chebyshev. i use FDATool to design bandpass filter. like this :
Fs = 16000; % Sampling Frequency
N = 16 * 4096; % Order
Fc1 = 15.48; % First Cutoff Frequency
Fc2 = 15.52; % Second Cutoff Frequency
flag = 'scale'; % Sampling Flag
SidelobeAtten = 100; % Window Parameter
% Create the window vector for the design algorithm.
win = chebwin(N+1, SidelobeAtten);
% Calculate the coefficients using the FIR1 function.
b = fir1(N, [Fc1 Fc2]/(Fs/2), 'bandpass', win, flag);
Hd = dfilt.dffir(b);
and use filter function in matlab to get my desire signal :
filteredSignal = filter(Hd.Numerator,1,y);
max_amp = max(filteredSignal );
and after filtering i get maximum of signal amplitude. it is work fine. but there is a big problem for me.
because of random noise i have diffrent values of filtered signal max.
like : 10.552 , 10.493 , 10.876 , 10.524 , 10.617
and when i decrease value of random noise like this :
r = 0.001;
y = x + x1 + r*randn(size(t)); % Sinusoids plus noise
i get this values of filterd signal maximum like :
10.541 , 10.541 ,10.541 , 10.541, 10.541
and now my question is :
how to reduce or decrease noise effect on my filtered signal? what should i do for get same value of max amplitude of signal after filtering?
thanks.
Fs = 16000; % Sampling frequency
fNy = Fs / 2;
T = 1/Fs; % Sample time
L = 200000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz , 5.8 , 12.6 , 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t) + sin(2*pi*5.8*t) + sin(2*pi*12.6*t);
x1 = 15.3 * sin(2*pi*15.5 * t );
a = 5.2;
b = 35.5;
r = a + (b-a).*rand(1,1);
y = x + x1 + r*randn(size(t)); % Sinusoids plus noise
N = 16 * 4096; % Order
Fc1 = 15.48; % First Cutoff Frequency
Fc2 = 15.52; % Second Cutoff Frequency
flag = 'scale'; % Sampling Flag
SidelobeAtten = 100; % Window Parameter
% Create the window vector for the design algorithm.
win = chebwin(N+1, SidelobeAtten);
% Calculate the coefficients using the FIR1 function.
b = fir1(N, [Fc1 Fc2]/(Fs/2), 'bandpass', win, flag);
Hd = dfilt.dffir(b);
filteredSignal = filter(Hd.Numerator,1,y);
Testim=1/15.5;
t0=0;
Ts=1/Fs;
steadyData=filteredSignal(100001:200000);
numAvr=100;
samLen=length(steadyData)/numAvr;
m=[];
for j=0:numAvr-1;
s0=steadyData(1+j*samLen:j*samLen+samLen);
Omega=fminbnd('jomega',(2*pi/Testim)*.9,(2*pi/Testim)*1.1, [0,1.0e-30 ], s0,t0,Ts);
[Amplitude,Theta,RMS]=sinefit2(s0,Omega,t0,Ts);
freq=Omega/(2*pi);
if 15.4<=freq & freq<=15.6
m=[m Amplitude];
else
end
end
avrAmp=(1/length(m))*sum(m)
I don't know what you want to do, exactly. But, in order to reduce noise effect hear, we could use least square method, if we know some information, such as frequency, sampling rate, etc. I give you an example.You can find the function 'sinefit2.m' at "http://www.mathworks.com/matlabcentral/fileexchange/3730-sinefit". I think, after fitting, the amplitude would be commonly more similar, but not always.
If you want to get always same amplitude, you should fix your random number. whenever you run your code, matlab will generate different random number, and SNR will be changed. The one way fixing random number is to initialize random number generator.
Please try following code.
Fs = 16000; % Sampling frequency
fNy = Fs / 2;
T = 1/Fs; % Sample time
L = 60000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz , 5.8 , 12.6 , 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t) + sin(2*pi*5.8*t) + sin(2*pi*12.6*t);
x1 = 15.3 * sin(2*pi*15.5 * t );
a = 5.2;
b = 35.5;
rng(0,'twister')
r = a + (b-a).*rand(1,1);
rng(0,'twister')
y = x + x1 + r*randn(size(t)); % Sinusoids plus noise
plot(y)
With these code you could see always the same signal y.

Low pass filtering not working as expected

The following Matlab script is for filtering a signal consisting of a 50 Hz and 120 Hz sine. I am calculating the frequnecy in rad/s as Fp= (2*PI * 30)/1000=0.184.
I have kept fp=0.184 and fst=0.185, as I want to filter out both 50 hz and 120 Hz.
But when I am plotting the FFT of the output of the filter, what I am getting is a sine at 50 Hz. Why this 50Hz sine is coming even after filtering?
Ideally there should not be any peak in the plot.
Before filtering
after filtering
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t =(0:L-1)*T; % Time vector
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid %
y = x + 2*randn(size(t)); % Sinusoids plus noise
y = x ;
plot(Fs*t(1:50),y(1:50));title('Signal');xlabel('time (milliseconds)')
%pause;
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);
% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)');xlabel('Frequency(Hz)');ylabel('|Y(f)|')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now let us see Low Pass Filtering of this signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Fp= (2*pi * 30)/1000; %=0.184 %only frequncies less than 30Hz will be passed
d=fdesign.lowpass('Fp,Fst,Ap,Ast',0.184,0.185,2,60);
designmethods(d);
Hd = design(d,'equiripple'); fvtool(Hd);
Filterd_Output = filter(Hd,y);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Filtered_Freq = fft(Filterd_Output,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
plot(f,2*abs(Filtered_Freq(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of Low Pass Filtered_Output')
xlabel('Frequency (Hz)');ylabel('|Filtered_Freq_Amplitude|')
Update
As suggested I compared the original spectrum with the filterd one. This explains me a lot. But is there any way so that I can reduce this spike at 50 Hz further?
You incorrectly specify the normalized frequencies for the filter. Matlab assumes the frequency to be in [0, 1], not in [0, pi].
Replace
d=fdesign.lowpass('Fp,Fst,Ap,Ast',0.184,0.185,2,60);
with
d=fdesign.lowpass('Fp,Fst,Ap,Ast', 2*30/Fs, 2*35/Fs,2,60);
or alternatively
d=fdesign.lowpass('Fp,Fst,Ap,Ast', 30, 35,2,60, Fs);
and it should work as expected.

Arguments in Plot function

The following Matlab scrip (taken from MATLAB help for fft) runs perfectly fine
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
But I am unable to understand why we needed Fs*t in plot(). Why I am making it dimension less?
Your vector t is defined in terms of samples, i.e. t(10) is the value taken as the 10th sample.
If you want to plot the signal vs. time, you will have to multiply the sampling instance with the sampling time, i.e. time = FS*t .
If you don't scale, you eventually plot the signal vs. the sampling instances. Then, however, the label "time(ms)" is not correct.