The Student Room Group

Matlab assignment

When making a sliding average filter on a data set in matlab. How do I plot the first cycle of the average?.
Reply 1
Can you upload what youve done for the filter?
Reply 2
Original post by mqb2766
Can you upload what youve done for the filter?

For an ECG graph I’ve applied a five point sliding average filter to the signal. I now have to generalise my code into a MATLAB function that will accept any arbitrary signal as the first input, and the filter length as the second input and return the smoothed signal as an output. How do I go about this? :smile:
Reply 3
Original post by Georgiaap
For an ECG graph I’ve applied a five point sliding average filter to the signal. I now have to generalise my code into a MATLAB function that will accept any arbitrary signal as the first input, and the filter length as the second input and return the smoothed signal as an output. How do I go about this? :smile:

Just write a function that returns the original signal as a start, then implement a loop in the functions body which averages "n/2" points either side of the current point. If its close to the start / end of the signal and the filtered signal must be the same length, adapt the filter length so that you dont overstep the signal boundaries.

As assignments go, it sounds pretty straightforward, so have a go and upload what you get if you have further questions.
Reply 4
Original post by mqb2766
Just write a function that returns the original signal as a start, then implement a loop in the functions body which averages "n/2 points either side of the current point. If its close to the start / end of the signal and the filtered signal must be the same length, adapt the filter length so that you dont overstep the signal boundaries.

As assignments go, it sounds pretty straightforward, so have a go and upload what you get if you have further questions

I’ve put
function[slidingavg]=Myfunction(ecg,5)
if i>len-(slidinglength-1)
disp(‘not valid’)

have I done the inputs wrong/ error checking ?
thankyou
Reply 5
Original post by Georgiaap
I’ve put
function[slidingavg]=Myfunction(ecg,5)
if i>len-(slidinglength-1)
disp(‘not valid’)

have I done the inputs wrong/ error checking ?
thankyou

Have you tried to run it?
You seemed to say you had it working for a 5 point filter.
Reply 6
Original post by mqb2766
Have you tried to run it?
You seemed to say you had it working for a 5 point filter.

Yes I’ve run it and nothing happens.
Do I need to generalise my sliding average first? Can I contact you on WhatsApp about this?
Reply 7
Original post by Georgiaap
Yes I’ve run it and nothing happens.
Do I need to generalise my sliding average first? Can I contact you on WhatsApp about this?

No for whatsapp and I can't see any sliding average so either you've not posted what youve done or don't understand it.

Id do:
* Create a function that simply returns the original vector and test it by creating a test vector and calling the function and verifying the return is the same vector.
* Modify the body of the function to loop over the original vector and create a copy of it, element by element, and return the copy
* Modify the loop to average 2 points either side (window 5) and return that
* Modify it to be a window of width 2n-1

If you do it something like that, you'll probably learn a bit of matlab at the same time as solving the problem. Upload what you attempt, including any test code to verify that what youve done works. The above isnt the most efficient, but it should be straightforward.
(edited 2 years ago)
Reply 8
Original post by mqb2766
No for whatsapp and I can't see any sliding average so either you've not posted what youve done or don't understand it.

Id do:
* Create a function that simply returns the original vector and test it by creating a test vector and calling the function and verifying the return is the same vector.
* Modify the body of the function to loop over the original vector and create a copy of it, element by element, and return the copy
* Modify the loop to average 2 points either side (window 5) and return that
* Modify it to be a window of width 2n-1

If you do it something like that, you'll probably learn a bit of matlab at the same time as solving the problem. Upload what you attempt, including any test code to verify that what youve done works. The above isnt the most efficient, but it should be straightforward.

Thanks
Original post by Georgiaap
Thanks

my sliding average filter:
slidingavg=[];
len=length(ecg_emg);
slidinglength=5;
for i = 1 : len-(slidinglength-1)
slidingavg(i)=(ecg_emg(i)+ecg_emg(i+1)+ecg_emg(i+2)+ecg_emg(i+3)+ecg_emg(i+4))/5


I now have to generalise my code into a MATLAB function that can take two inputs; first - the signal data, second - filter length. and outputs the smoothed signal. My code for this so far is:
function[slidingavg]=G_function(ecg_emg,fl)
if isinteger(fl)
if isnumeric(ecg_emg)
len=length(ecg_emg)
for l=1:len-fl
p=0
for m=0:5-1
p=(p+ecg_emg(l+m))
end
slidingavg(l)=p/l

i get an error displayed and i'm not sure why? Any help would be appreciated.
end
else
print('yes')
end
else
print('databad')
end
Note - you've spammed this across 5 threads. Pls delete the other ones.

What is the error?
You've got 5s still existing in your function and is the average calculation correct etc ...?
(edited 2 years ago)
Reply 11
Original post by Georgiaap
When making a sliding average filter on a data set in matlab. How do I plot the first cycle of the average?.

Here's a basic example assuming a simple dataset and a window width of 5:

% Let's say your data is:
data = rand(1,100); % Random data for example

% Choose your window width:
windowWidth = 5; % Change according to your needs

% Compute sliding (moving) average:
avg = movmean(data, windowWidth);

% Plot first cycle of the average:
figure
plot(data(1:windowWidth), 'k') % Original data in black
hold on
plot(avg(1:windowWidth), 'r') % Moving average in red
legend('Original Data','Moving Average')

Quick Reply

Latest

Trending

Trending