I am trying to implement the following system in MATLAB.
I am reading an audio signal and want to perform the operation below.
so far I have done the following:
%read the audio file
[y,Fs] = audioread('input_original.wav');
syms k x
yx = symsum(k, k, -inf, y);
%write the output
audiowrite('signal_divbb.wav',yx,Fs,'BitsPerSample',64)
is there a way to implement this signal properly? I am not familiar with MATLAB
The output of audioread (your y variable) has m rows times n columns, where m is the signal length and n is the number of channels (2 for stereo). You can use cumsum as follows:
yx = cumsum(y, 1);
This computes the cumulative sum along the first dimension (i.e. time). So yx(k,c) equals y(1,c)+y(2,c)+...+y(k,c), where k ranges from from 1 to m, and c is the channel index from 1 to n.
Related
I'm using the FastIca toolbox (https://research.ics.aalto.fi/ica/fastica/) but am confused about the orientation of the resulting W (separating/unmixing) matrix.
Let X be a n x B matrix where n is the number of signals in a data set and B is the number of time points sampled at.
I've been calculating the W matrix using:
[A,W] = fastica(X)
However, because W is a n x n matrix I can't tell how it is oriented and whether to use W or its transpose (W.') in subsequent calculations, and can't seem to find a clear answer in the documentation.
To help me get my bearings, is fastica maximising the independence in the rows of Y where Y = W.'X or Y = WX?
It should be Y=W*X. To be sure, you can reduce the number of component to estimate and then W should no longer be square:
[A,W] = fastica(X,'numOfIC',2)
If W is of size 2xn then Y=W*X. Else W has a size nx2 and Y=W'*X
I have a matrix of 50-by-1 that is demodulated data. As this matrix has only one element in each row, I want to repeat this only element 16 times in each row so the matrix become 50 by 16. I did it using the repmat(A,16) command in Matlab. Now at receiving end noise is also added in matrix of 50 by 16. I want to get it back of 50 by 1 matrix. How can I do this?
I tried averaging of all rows but it is not a valid method. How can I know when an error is occurring in this process?
You are describing a problem of the form y = A * x + n, where y is the observed data, A is a known linear transform, and n is noise. The least squares estimate is the simplest estimate of the unknown vector x. The keys here are to express the repmat() function as a matrix and the observed data as a vector (i.e., a 50*16x1 vector rather than a 50x16 matrix).
x = 10 * rand(50,1); % 50x1 data vector;
A = repmat(eye(length(x)),[16,1]); % This stacks 16 replicas of x.
n = rand(50*16,1); % Noise
y = A * x + n; % Observed data
xhat = A \ y; % Least squares estimate of x.
As for what the inverse (what I assume you mean by 'reverse') of A is, it doesn't have one. If you look at its rank, you'll see it is only 50. The best you can do is to use its pseudoinverse, which is what the \ operator does.
I hope the above helps.
I know that ifft sums multiple sine waves up from data obtain from doing an fft on a signal. is there a way to do a ifft using square waves instead of sine waves?
I'm not trying to get the original signal back but trying to rebuild it using square waves from the data taken from the fft instead of the normal sine wave summation process.
See simple example below: the signals I will be using are human audio signals about 60 seconds long so I'm trying to see if I can use / alter the ifft command in some way.
PS: I'm using Octave 4.0 which is similar to Matlab
clear all,clf reset, clc,tic
Fs = 200; % Sampling frequency
t=linspace(0,1,Fs);
freq=2;
%1 create signal
ya = .5*sin(freq*pi*2*t+pi);
%2 create frequency domain
ya_fft = fft(ya);
%3 rebuild signal
mag = abs(ya_fft);
phase = unwrap(angle(ya_fft));
ya_newifft=ifft(mag.*exp(i*phase));
ifft_sig_combined_L1=ifft(mag.*exp(i*phase),Fs); %use Fs to get correct file length
% square wave
vertoffset=0.5;
A=1
T = 1/freq; % period of the signal
square = mod(t * A / T, A) > A / 2;
square = square - vertoffset;
subplot(3,1,1);
plot(t,ya,'r')
title('orignal signal')
subplot(3,1,2);
plot(t,ifft_sig_combined_L1)
title('rebuilt signal')
subplot(3,1,3);
plot(t,square)
title('rebuilt signal with square wave')
Define the basis vectors you want to use and let them be the columns of a matrix, A. If b is your signal, then just get the least squares solution to Ax = b. If A is full rank, then you will be able to represent b exactly.
Edit:
Think about what a matrix-vector product does: Each column of the matrix is multiplied by the corresponding element of the vector (i.e., the n^th column of the matrix is multiplied by the n^th element of the vector) and the resulting products are summed together. (This would be a lot easier to illustrate if this site supported latex.) In Matlab, a horrible but hopefully illustrative way to do this is
A = some_NxN_matrix;
x = some_Nx1_vector;
b = zeros( size(A,1), 1 );
for n = 1 : length(x)
b = b + A(:,n) * x(n);
end
(Of course, you would never actually do the above but rather b = A*x;.)
Now define whatever square waves you want to use and assign each to its own Nx1 vector. Call these vectors s_1, s_2, ..., s_M, where M is the number of square waves you are using. Now let
A = [s1, s2, ..., s_M];
According to your question, you want to represent your signal as a weighted sum of these square waves. (Note that this is exactly what a DFT does it just uses orthogonal sinusoids rather than square waves.) To weight and sum these square waves, all you have to do is find the matrix-vector product A*x, where x is the vector of coefficients that weight each column (see the above paragraph). Now, if your signal is b and you want to the find the x that will best sum the square waves in order to approximate b, then all you have to do is solve A*x=b. In Matlab, this is given by
x = A \ b;
The rest is just linear algebra. If a left-inverse of A exists (i.e., if A has dimensions M x N and rank N, with M > N), then (A^-1) * A is an identity matrix and
(A^-1) * A * x = (A^-1) * b,
which implies that x = (A^-1) * b, which is what x = A \ b; will return in Matlab. If A has dimensions M x N and rank M, with N > M, then the system is underdetermined and a left-inverse does not exist. In this case you have to use the psuedo-inverse to solve the system. Now suppose that A is NxN with rank N, so that both the left- and right-inverse exist. In this case, x will give an exact representation of b:
x = (A^-1) * b
A * x = A * (A^-1) * b = b
If you want an example of A that uses square waves to get an exact representation of the input signal, check out the Haar transform. There is a function available here.
I'm new to programming in Matlab. I'm trying to figure out how to calculate the following function:
I know my code is off, I just wanted to start with some form of the function. I have attempted to write out the sum of the function in the program below.
function [g] = square_wave(n)
g = symsum(((sin((2k-1)*t))/(2k-1)), 1,n);
end
Any help would be much appreciated.
Update:
My code as of now:
function [yout] = square_wave(n)
syms n;
f = n^4;
df = diff(f);
syms t k;
f = 1; %//Define frequency here
funcSum = (sin(2*pi*(2*k - 1)*f*t) / (2*k - 1));
funcOut = symsum(func, v, start, finish);
xsquare = (4/pi) * symsum(funcSum, k, 1, Inf);
tVector = 0 : 0.01 : 4*pi; %// Choose a step size of 0.01
yout = subs(xsquare, t, tVector);
end
Note: This answer was partly inspired by a previous post I wrote here: How to have square wave in Matlab symbolic equation - However, it isn't quite the same, which is why I'm providing an answer here.
Alright, so it looks like you got the first bit of the question right. However, when you're multiplying things together, you need to use the * operator... and so 2k - 1 should be 2*k - 1. Ignoring this, you are symsuming correctly given that square wave equation. The input into this function is only one parameter only - n. What you see in the above equation is a Fourier Series representation of a square wave. A bastardized version of this theory is that you can represent a periodic function as an infinite summation of sinusoidal functions with each function weighted by a certain amount. What you see in the equation is in fact the Fourier Series of a square wave.
n controls the total number of sinusoids to add into the equation. The more sinusoids you have, the more the function is going to look like a square wave. In the question, they want you to play around with the value of n. If n becomes very large, it should start approaching what looks like to be a square wave.
The symsum will represent this Fourier Series as a function with respect to t. What you need to do now is you need to substitute values of t into this expression to get the output amplitude for each value t. They define that for you already where it's a vector from 0 to 4*pi with 1001 points in between.
Define this vector, then you'll need to use subs to substitute the time values into the symsum expression and when you're done, cast them back to double so that you actually get a numeric vector.
As such, your function should simply be this:
function [g] = square_wave(n)
syms t k; %// Define t and k
f = sin((2*k-1)*t)/(2*k-1); %// Define function
F = symsum(f, k, 1, n); %// Define Fourier Series
tVector = linspace(0, 4*pi, 1001); %// Define time points
g = double(subs(F, t, tVector)); %// Get numeric output
end
The first line defines t and k to be symbolic because t and k are symbolic in the expression. Next, I'll define f to be the term inside the summation with respect to t and k. The line after that defines the actual sum itself. We use f and sum with respect to k as that is what the summation calls for and we sum from 1 up to n. Last but not least, we define a time vector from 0 to 4*pi with 1001 points in between and we use subs to substitute the value of t in the Fourier Series with all values in this vector. The result should be a 1001 vector which I then cast to double to get a numerical result and we get your desired output.
To show you that this works, we can try this with n = 20. Do this in the command prompt now:
>> g = square_wave(20);
>> t = linspace(0, 4*pi, 1001);
>> plot(t, g);
We get:
Therefore, if you make n go higher... so 200 as they suggest, you'll see that the wave will eventually look like what you expect from a square wave.
If you don't have the Symbolic Math Toolbox, which symsum, syms and subs relies on, we can do it completely numerically. What you'll have to do is define a meshgrid of points for pairs of t and n, substitute each pair into the sequence equation for the Fourier Series and sum up all of the results.
As such, you'd do something like this:
function [g] = square_wave(n)
tVector = linspace(0, 4*pi, 1001); %// Define time points
[t,k] = meshgrid(tVector, 1:n); %// Define meshgrid
f = sin((2*k-1).*t)./(2*k-1); %// Define Fourier Series
g = sum(f, 1); %// Sum up for each time point
end
The first line of code defines our time points from 0 to 4*pi. The next line of code defines a meshgrid of points. How this works is that for t, each column defines a unique time point, so the first column is 200 zeroes, up to the last column which is a column of 200 4*pi values. Similarly for k, each row denotes a unique n value so the first row is 1001 1s, followed by 1001 2s, up to 1001 1s. The implications with this is now each column of t and k denotes the right (t,n) pairs to compute the output of the Fourier series for each time that is unique to that column.
As such, you'd simply use the sequence equation and do element-wise multiplication and division, then sum along each individual column to finally get the square wave output. With the above code, you will get the same result as above, and it'll be much faster than symsum because we're doing it numerically now and not doing it symbolically which has a lot more computational overhead.
Here's what we get when n = 200:
This code with n=200 ran in milliseconds whereas the symsum equivalent took almost 2 minutes on my machine - Mac OS X 10.10.3 Yosemite, 16 GB RAM, Intel Core i7 2.3 GHz.
I'm implementing an algorithm where I need to compute the linear convolution and cross-correlation between two 1D vectors a and b using the FFT. If the length of a is m and the length of b is n, then the total length of the resulting convolution will be m + n - 1.
However, the algorithm requires the output of the convolution to be the same length as the inputs. Since m = n, the input vectors are the same length.
In Matlab, given that both vectors have been zero-padded to length m + n - 1, the convolution is computed as:
ifft(fft(a).*fft(b))
Alternately, the cross-correlation between the two vectors is computed as:
fftshift(ifft(fft(a).*conj(fft(b))))
The output is of length m + n - 1. I need to zero-pad the vectors to ensure that circular convolution does not occur when using the FFT.
However, I would like the output length to be the same as the length of the inputs. An associated question (also on stackoverflow) shows how the correlation of two images can be trimmed.
How do I trim the 1D output vector so that it is the same length as the input vectors?
In MATLAB, conv(a, b, 'same') returns the central part of the convolution that is the same size as a. For cross correlation, you can use xcorr, which will give you the cross correlation result. I guess you need to crop the result by yourself to get the same size as input.
there are formulas:
c = a conv b;
where, a, b and c are in time domain; After trans to frequency domain, the formula change to
C = A * B;
where, A, B and C are fft result of a, b, and c respectively, and all of them are in frequency domain; so c = ifft(C);