MATLAB Answers

FFT OF A SIGNAL

25 views (last 30 days)
Abbas Alawie
Abbas Alawie on 17 Dec 2020
Answered: Subhadeep Koley on 17 Dec 2020
I have a signal data set that i have to analyse but i cant get my code to work can anyonbe help me? Am i forgetting to include something or add something
thats the error that im getting
Error: File: fft_fun.m Line: 39 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "fft_fun".)
The code is as follows
function [PN, df] = fft_fun(fs, input_signal)
function [PN, df] = fft_fun(fs, input_signal)
%% Fast Fourier Transform (FFT); Powerline Noise (tut1)
% Function to compute and plot the fft (fast fourier transform) of an input
% signal.
% Input_signal = signal to compute the fft on. fs = sample rate of
% the input signal. If sample rate is unknown, it can be found using:
% fs = 1/(max(tm)/length(tm)-1).
% A fft plots the signal in the frequency spectrum, allowing for the easy
% recognition of power line noise, usually shown at either 50Hz or 60Hz in
% the plotted frequency transform.
%% create FFT
% Detrend of Input Signal
n = detrend(input_signal);
% FFT
N = fft(n);
% Power Spectrum
spect = 2*abs(N)/length(N);
PN = spect.^2;
% Frequency Axis
df = fs/length(n); %% fs << fc
f = 0:df:fs-df; %% fs << fc
indf = (1:floor(length(f)/2));
figure
plot(f(indf),PN(indf));
xlabel('Hz')
ylabel('(Input Units)^2 / Hz')
end % end of function fft_fun
Appendix 2b - Power-line Nose & High-Frequency Noise Removal (notch_fun.m)
function [signal] = notch_fun(input_signal)
%% Powerline Noise and High Frequency Noise Removal (tut3)
% Function to apply a 50Hz notch filter and low and high pass filters.
% Notch filter removes powerline noise at specified frequency (50Hz).
% Low pass filter removes high frequency noise
% input_signal = signal to apply filters to.
% [signal] = output signal with applied filters (noise removed).
%% Notch Filter @ 50Hz
fc = 1000;
fQf = 35;
NOf0 = 0;
bw = 0;
signal = input_signal;
while NOf0<fc/2-50,
% Apply a 50Hz and harmonics notch filters:
% notch frequency
NOf0 = NOf0+50;
% normalise frequency in 'pi radiant per sample'
wNO = NOf0/(fc/2);
bw = wNO/fQf;
[bNO,aNO] = iirnotch(wNO,bw);
% Apply filter
signal = filter(bNO,aNO,signal);
end;
%% Build a 40 Hz low pass filter against the HF noise
% normalise frequency in 'pi radiant per sample'
order = 50; % Order 50
NOf0 = 40;
wNO = NOf0/(fc/2);
bNO = fir1(order,wNO); % defaults to lowpass filter
% Apply low pass filter
signal = filter(bNO,1,signal);
%% Apply High-Pass Filter at 1Hz
order = 50;
NOf0 = 1;
wNO = NOf0/(fc/2);
bNO = fir1(order,wNO,'high');
% Apply high pass filter
signal = filtfilt(bNO,1,signal);
%% Plot Signals
% Plot original signal and signal of notch filtered signal for comparison
figure
% Plot of original signal
subplot(2,1,1)
plot(input_signal)
title('Input ECG Signal')
% Plot of output signal
subplot(2,1,2)
plot(signal)
title('Notch Filtered Signal')
end % end of function notch_fun
Appendix 2c - Baseline Wandering Removal (blw_fun.m)
function [filtsignal] = blw_fun(tm,input_signal)
%% Low-Pass Filter; DC Removal Filter; Baseline Wandering (tut3)
% Function to remove the baseline wandering of an input signal.
% Takes a signal, input_signal, and applies a low-pass filter to remove DC
% noise, as such removes baseline wandering. Allows for easier analysis of
% mutiple-lead ECG signals (typically 12-Lead ECGs) by removing overlap of
% individual lead signals.
%% Low-Pass Filter
% create the DC removal filter coefficient
lamda = 0.98; % as close to but not equalling 1
b = [1 -1];
a = [1 -lamda];
figure
freqz(b,a); % plot the freqency response of the low-pass filter
title('Filter Frequency Response')
% filter input signal using DC removal filter
filtsignal = filtfilt(b,a,input_signal);
%% Comparison of Signals
% plot filtsignal; filtered signal using filtfilt
figure
subplot(2,1,2)
plot(tm,filtsignal);
title('Baseline Wandering Corrected ECG Signal')
% plot original signal
subplot(2,1,1)
plot(tm,input_signal);
title('Input ECG Signal')
end % end of function blw_fun 
Appendix 2d - Calculate SNR (snr_fun.m)
function [Pnoise, Psignal, SNR] = snr_fun(PN,df)
%% Signal to Noise Ratio (SNR) (tut4)
% Function to calculate and display the signal to noise ratio of an ECG
% signal. Also displays the values of the noise and signals individually.
% Takes PN and df input obtained from the completion of the fft function
% (fft_fun.m) to calculate the noise of a signal and determine a numeric
% value of the signal to compute the SNR.
% Pnoise = amount of noise in the signal
% Psignal = numeric value of signal
% SNR = calucated Signal to Noise Ratio
% Power of the noise
Pnoise = sum(PN(1:round(5/df)))/round(5/df) + sum(PN(1+round(15/df):end))/(round(500/df)-(1+round(15/df))) + PN(round(50/df));
% Power of the signal
Psignal = sum(PN(1+round(5/df):round(15/df)));
% Calculate the signal to noise ratio (SNR)
SNR = 10*log10(Psignal/Pnoise);
end % end of snr_fun

  0 Comments

Sign in to comment.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 17 Dec 2020
Hi, there are several lines (e.g. line 39, line 110, etc.) in your code, which start with the word "Appendix". Those lines are not valid MATLAB syntaxes. Either you can comment out those lines or remove them from the file.

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!