How can I calculate the moving average and filtering
1 view (last 30 days)
Show older comments
Hi Everybody,
Yes this is my home work, need some help on it, please!
I have tried to use filter, but I got an error on it.
% calculate 2 sample running average? Is it where need to H(z) = Y(z)/X(z) ? How can I do it with this code?
x = impulse and x = zeros, how? Why?
This is so comfusing...
The input x(n0 is an impulse, and the out y(n) is the impulse response.
Adapt the program to implement a filter of the form
y(n)=0.5 x(n) + 0.5 y(n-1)
Try increasing the delay to say 100 or more e.g. (samplesCounter-100) and
listening to the impulse response by uncommenting the line at the end
soundsc(y,8000). You will need to increase the start sample by one more than
the delay, in this case 101:
for samplesCounter=101:signalEnd-signalStart
signalStart = -2000;
signalEnd = 2000;
impulse_Position = 0;
samples = (signalStart:signalEnd);
x = (samples==impulse_Position);
y = zeros(1,signalEnd-signalStart+1); %initialise output y(n) as zeros
% x = zeros(1,20);
% initialise input x(n) as zeros
% x(1,2)=1; %Set x as an impulse delayed by one sample
for samplesCounter = 2:signalEnd-signalStart % start at n=2 because we are using n-1 as the index below
% y(samplesCounter)=0.5*x(samplesCounter)+0.5*x(samplesCounter-1); % calculate 2 sample running average
y(samplesCounter)=0.5*x(samplesCounter)+0.5*y(samplesCounter-1);
end
subplot(2,1,1);
stem(samples,x);
ylabel('Input x(n)');
xlabel('Sample number');
subplot(2,1,2);
stem(samples,y);
grid;
xlabel('Sample number');
ylabel('Output y(n)');
grid on;
%soundsc(y,8000);
0 Comments
Answers (1)
Image Analyst
on 13 Dec 2020
You need to make a list of all the starting positions to reflect the fact that you're going to move along by samplesCounter, not by 1 which your for loop is doing now (if that's what they want - it's a bit hard to tell). Here's a hint:
y = rand(1, 5000);
samplesCounter = 100; % whatever...
indexes = 1 : samplesCounter : length(y);
for k = 2 : length(indexes)
index1 = indexes(k-1);
index2 = indexes(k) - 1;
% Option 1 : Everything in between, inclusive.
theSum1 = mean(y(index1:index2));
% Option 2 : Sum y only at those two indexes, and not everything in between.
theSum2 = 0.5 * y(index1) + 0.5 * y(index2);
fprintf('Analyzing singnal between index %d and %d. theSum1 = %.2f. theSum2 = %.2f\n', ...
index1, index2, theSum1, theSum2);
end
4 Comments
Image Analyst
on 16 Dec 2020
I'm not sure you transcribed the problem correctly, like
Try increasing the delay to say 100 or more e.g. (samplesCounter-100)
For example why is there a minus sign there? Should it really be
Try increasing the delay to say 100 or more e.g. (samplesCounter = 100)
And when it says to increase the delay to 100 does it mean 2, then 3, then 4, then 5 and all possibilities? Or just 2 and then 100? If it's just 2 and 100, then why did it put samplesCounter in a for loop, like
for samplesCounter=101:signalEnd-signalStart
Please try to give us the exact wording so we can correct your loops and coding.
See Also
Categories
Find more on Audio Processing Algorithm Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!