Clear Filters
Clear Filters

How do I fix this code with some protting and defining errors?

1 view (last 30 days)
I have a project but I'm not sure what's wrong really with the code that I have. Section 1 through 5 seem good (but if you see anything glaringly wrong please feel free to tell me but my problem isn't for those sections. Section 6 just isn't seeming to run well and I am sick of looking at this code so i just need other eyes on it pls.
The prompt for section 6 is: "Design a filter using window-based filter design to selectively remove the noise. Use the built-in function fir1. Apply the filter to each signal while experimenting with different cutoff frequencies. Plot the selected filter impulse response and each of the filtered signals. What kind of filter was selected and what were the cutoff frequencies?"
The code is:
%{
Signals and Systems
Project 3
Names
%}
close all; clear; clc
% Part 1 - plot magnitude spectrum of x(t) over [0,w] =====================
% Variables and equations
w = 0:(pi/1000):pi;
Xw = (-j./w) .* (1 - exp(-100j.*w));
% Plot magnitude spectra
figure
plot(w, abs(Xw))
xlabel('Frequency ω'); ylabel('|X(ω)|'); title('Magnitude Spectrum of X(ω)')
% Part 2 - FFT plot =======================================================
% Variables and equations
Ts=1; t=0:Ts:100; N = 1024;
xtn = heaviside(t) - heaviside(t-100);
DFT1 = fft(xtn, N);
wTs = linspace(0,2*pi,N);
% Plot DFT magnitude spectrum
figure
plot(wTs, abs(DFT1))
xlabel('ωTs'); ylabel('|X(ω)|'); title('DFT Magnitude Spectrum')
axis('tight')
% Part 3 - FFT plot for different N =======================================
% N=64
N=64;
wTs = linspace(0,2*pi,N);
DFT2 = fft(xtn, N);
% Subplot for N=64
figure
sgtitle('DFT Magnitude Spectra for Different N')
subplot(3,1,1)
plot(wTs, abs(DFT2));
xlabel('ωTs'); ylabel('|X(ω)|'); title('N=64')
axis('tight')
% N=128
N=128;
wTs = linspace(0,2*pi,N);
DFT3 = fft(xtn, N);
% Subplot for N=128
subplot(3,1,2)
plot(wTs, abs(DFT3));
xlabel('ωTs'); ylabel('|X(ω)|'); title('N=128')
axis('tight')
% N=512
N=512;
wTs = linspace(0,2*pi,N);
DFT4 = fft(xtn, N);
% Subplot for N=64
subplot(3,1,3)
plot(wTs, abs(DFT4));
xlabel('ωTs'); ylabel('|X(ω)|'); title('N=512')
axis('tight')
% Part 4 - Phase and magnitude spectra of clean AP signal =================
% Data, variables, equations
apdata = load('apdata.mat');
Ts=0.0796; ws=1/(2*Ts);
fftC = fft(apdata.clean(:,1));
magC = abs(fftC); phaseC = angle(fftC);
wTsC = linspace(0, ws, length(magC));
figure
sgtitle('DFT Spectra for Clean Action Potential')
subplot(2,1,1)
plot(wTsC, magC)
xlabel('ωTs'); ylabel('|X(ω)|'); title('Magnitude Spectrum')
xlim([0,ws])
subplot(2,1,2)
plot(wTsC, phaseC)
xlabel('ωTs'); ylabel('<X(ω)'); title('Phase Spectrum')
xlim([0,ws])
% Part 5 - Phase and magnitude spectra of noisy AP signal =================
% Data, variables, equations
fftN = fft(apdata.noisy(:,1));
magN = abs(fftN); phaseN = angle(fftN);
wTsN = linspace(0, ws, length(magN));
figure
sgtitle('DFT Spectra for Noisy Action Potential')
subplot(2,1,1)
plot(wTsN, magN)
xlabel('ωTs'); ylabel('|X(ω)|'); title('Magnitude Spectrum')
xlim([0,ws])
subplot(2,1,2)
plot(wTsN, phaseN)
xlabel('ωTs'); ylabel('<X(ω)'); title('Phase Spectrum')
xlim([0,ws])
% Part 6 - Filter design ============ THIS IS THE PART I NEED HELP WITH PLS
cutoff_frequencies = [30, 5, 15, 50]; % Cutoff frequencies to test
filtered_signals = cell(length(cutoff_frequencies), 1); % Cell array to store filtered signals
for i = 1:length(cutoff_frequencies)
% Design filter
cutoff = cutoff_frequencies(i)/125; % Normalize cutoff frequency
b = fir1(50, [1/125 cutoff]);
%Plotting the impulse response
figure;
stem(b, 'filled');
xlabel('Sample Index');
ylabel('h[n]');
title('Impulse Response of the Filter');
% Apply filter to noisy signal
filtered_signals{i} = filter(b, 1, apdata.noisy(:,1));
% Plot filtered signal and compare with clean signal
figure;
plot(wTsC, magC, '-r', 'DisplayName', 'Clean'); % Plot the clean signal
hold on;
plot(wTsN, magN, '-k', 'DisplayName', 'Noisy'); % Plot the noisy signal
plot(wTsN, abs(filtered_signals{i}), '-b', wTsN,(apdata.noisy(:,1)), '-r');
title(sprintf('Filtered Signal (cutoff = %d Hz)',cuttoff_frequencies(i)));
end
% Plotting the filtered signals
figure(7)
subplot(4,1,1)
plot(wTsN, abs(filtdirty1))
xlabel('w')
ylabel('|X(w)|')
title('Filtered Signal - Cutoff = 30Hz')
subplot(4,1,2)
plot(wTsN, abs(filtdirty2))
xlabel('w')
ylabel('|X(w)|')
title('Filtered Signal - Cutoff = 5Hz')
subplot(4,1,3)
plot(wTsN, abs(filtdirty3))
xlabel('w')
ylabel('|X(w)|')
title('Filtered Signal - Cutoff = 15Hz')
subplot(4,1,4)
plot(wTsN, abs(filtdirty4))
xlabel('w')
ylabel('|X(w)|')
title('Filtered Signal - Cutoff = 50Hz')
% Plotting the impulse response of the filters
figure(8)
subplot(4,1,1)
impz(b1)
title('Impulse Response of Filter - Cutoff = 30Hz')
subplot(4,1,2)
impz(b2)
title('Impulse Response of Filter - Cutoff = 5Hz')
subplot(4,1,3)
impz(b3)
title('Impulse Response of Filter - Cutoff = 15Hz')
subplot(4,1,4)
impz(b4)
title('Impulse Response of Filter - Cutoff = 50Hz')
  1 Comment
Luca Ferro
Luca Ferro on 18 Apr 2023
Edited: Luca Ferro on 18 Apr 2023
could you share the file apdata.mat?
and what do you mean with ' isn't seeming to run well'?

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!