100 lines
2.0 KiB
Matlab
100 lines
2.0 KiB
Matlab
pkg load signal;
|
|
|
|
signal = csvread('unknownsignal.csv');
|
|
#Create a window for the domain we want to study
|
|
domainStart = 100;
|
|
domainEnd = 400;
|
|
|
|
lengthDomain = domainEnd - domainStart +1;
|
|
#window the signal
|
|
signal_window = zeros(size(signal));
|
|
signal_window(domainStart:domainEnd) = signal(domainStart:domainEnd) .* blackman(lengthDomain)';
|
|
|
|
#Plot the two raw signals
|
|
figure;
|
|
subplot(1,2,1);
|
|
plot(signal);
|
|
title("Signal");
|
|
xlabel("samples");
|
|
ylabel("Amplitude");
|
|
|
|
subplot(1,2,2);
|
|
plot(signal_window);
|
|
title("Blackman signal windowing");
|
|
xlabel("samples");
|
|
ylabel("Gain");
|
|
|
|
#Apply FFT to both signals.
|
|
signalFFT = fft(signal);
|
|
signalFFTwindow = fft(signal_window);
|
|
fs=600;
|
|
F = (0:length(signal)-1)*(fs/length(signal));
|
|
|
|
figure;
|
|
subplot(1,2,1);
|
|
plot(F,signalFFT);
|
|
title("Signal FFT");
|
|
xlabel("Frequency");
|
|
ylabel("Gain");
|
|
|
|
subplot(1,2,2);
|
|
plot(F,signalFFTwindow);
|
|
title("Blackman signal FFT");
|
|
xlabel("Frequency");
|
|
ylabel("Gain");
|
|
|
|
|
|
order = 50;
|
|
fc = 100; % cutoff frequency
|
|
h_fir1 = fir1(order, fc/(fs/2));
|
|
|
|
n = 4;
|
|
Wn = fc/(fs/2);
|
|
[b, a] = butter(n, Wn);
|
|
|
|
|
|
% filter the signals
|
|
filtered_signal = filter(h_fir1, 1, signal);
|
|
filtered_windowed_signal = filter(h_fir1, 1, signal_window);
|
|
filtered_signal_butter = filter(b, a, signal);
|
|
filtered_windowed_signal_butter = filter(b, a, signal_window);
|
|
|
|
% plot the signals using subplots
|
|
t = linspace(0, length(signal)/fs, length(signal));
|
|
t_window = linspace(0, length(signal)/fs, length(signal));
|
|
|
|
subplot(2,2,1);
|
|
plot(t, signal);
|
|
title('Original Signal');
|
|
|
|
subplot(2,2,2);
|
|
plot(t_window, signal_window);
|
|
title('Windowed Signal');
|
|
|
|
subplot(2,2,3);
|
|
plot(t, filtered_signal);
|
|
title('FIR1 Filtered Signal');
|
|
|
|
subplot(2,2,4);
|
|
plot(t_window, filtered_windowed_signal);
|
|
title('FIR1 Filtered Windowed Signal');
|
|
|
|
figure;
|
|
|
|
subplot(2,2,1);
|
|
plot(t, signal);
|
|
title('Original Signal');
|
|
|
|
subplot(2,2,2);
|
|
plot(t_window, signal_window);
|
|
title('Windowed Signal');
|
|
|
|
subplot(2,2,3);
|
|
plot(t, filtered_signal_butter);
|
|
title('Butterworth Filtered Signal');
|
|
|
|
subplot(2,2,4);
|
|
plot(t_window, filtered_windowed_signal_butter);
|
|
title('Butterworth Filtered Windowed Signal');
|
|
|