Skip to content Skip to navigation

OpenStax_CNX

You are here: Home » Content » Code

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • Rice University ELEC 301 Projects

    This module is included inLens: Rice University ELEC 301 Project Lens
    By: Rice University ELEC 301As a part of collection: "ELEC 301 Projects Fall 2009"

    Click the "Rice University ELEC 301 Projects" link to see all content affiliated with them.

Also in these lenses

  • Lens for Engineering

    This module is included inLens: Lens for Engineering
    By: Sidney BurrusAs a part of collection: "ELEC 301 Projects Fall 2009"

    Click the "Lens for Engineering" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.
 

Code

Module by: Graham de Wit, Nicholas Newton, Grant Cathcart. E-mail the authors

Summary: The MATLAB source code for the Sparse Signal Recovery in the Presence of Noise collection.

Code

The following is the MATLAB source code for each of the components of our project.

addNoise.m



function out = addNoise(sig,mean,sd,Plot)
%addNoise
%adds noise with given mean and sd to the signal
    rand=randn(1,1000)*sd+mean;
    out=sig+rand;
    if(Plot==1)
        plot(1:1000,out,1:1000,sig);
    end
end

sample.m


function out = sample(sd,plot,sig)
%sample
%samples a manually constructed signal, and adds gaussian noise to it
%with a standard deviation that is provided
out=fft(addNoise(sig,0,sd,plot));
end

init.m

function [out,samp]=init(sig,sd)
    %averages the signal and the noise over a number of samples to make the
    %noise level manageable
    out=sig+randn(1,1000).*sd;
    %optimize number of samples
    if sd<76
        val=6.25;
    else
        val=9;
    end
    samp=floor((ceil(sd))^2/(val));
    for n=2:samp
        out=(out.*(n-1)+sig+randn(1,1000).*sd)/n;
    end
    out=fft(out);
end

simpleIterate.m

function [mask, NSig,runT] = simpleIterate(sigMask,threshold,run,n,sd,sig)
%simpleIterate(sigMask,threshold,run,n)
%computes an iteration of the thresholding, with a running average of run,
%on iteration n, with the current signal mask of sigMask
%returns the new signal mask, the current signal(non-masked) NSig and the
%running average of the signal runT
    siz=size(sigMask);
    temp=zeros(1,siz(2));
    for i=1:4
        NSig=sample(sd,0,sig).*sigMask;
        if(n==1)
            runT=NSig;
        else
            runT=(run.*(n-1)+NSig)/n;
        end
        %temp=temp+(max(abs(real(NSig)),abs(imag(NSig)))>threshold);
        temp=temp+(max(abs(real(runT)),abs(imag(runT)))>threshold);
        %temp=temp+(abs(NSig)>threshold);
    end
    mask=zeros(1,siz(2));
    for l=1:siz(2)
        if(temp(l)<2)
            mask(l)=0;
        else
            mask(l)=sigMask(l);
        end
    end
end


testArbitary.m

function [flag,samples,time]=testArbitrary(sig,sd)
%Simulates the transmission of a signal in the library, and tests whether
%or not it can be recovered.
    siglib=cat(1,sin(0:pi/500:(1000*pi-1)/500),sin(0:pi/250:(2000*pi-1)/500),sin(0:pi/125:(4000*pi-1)/500),sin(0:pi/50:(10000*pi-1)/500),sin(0:pi/25:(20000*pi-1)/500));
    siglib=cat(1,siglib,sin(0:pi/500:(1000*pi-1)/500)+sin(0:pi/50:(10000*pi-1)/500),cos(0:pi/500:(1000*pi-1)/500),cos(0:pi/250:(2000*pi-1)/500),cos(0:pi/125:(4000*pi-1)/500),cos(0:pi/50:(10000*pi-1)/500));
    siglib=cat(1,siglib,cos(0:pi/25:(20000*pi-1)/500),cos(0:pi/25:(20000*pi-1)/500)+sin(0:pi/500:(1000*pi-1)/500),sin(0:pi/500:(1000*pi-1)/500)+sin(0:pi/125:(4000*pi-1)/500)+cos(0:pi/25:(20000*pi-1)/500));
    sigmax=max(abs(fft(siglib(sig,:))));
    threshhold=sigmax-3*max(abs(real(fft(randn(1,1000)))));
    tolerance=.5;
    A=ones(1,1000);
    flag=0;
    tic
    [C,samples]=init(siglib(sig,:),sd);
    for i=1:10000
        [A,B,C]=simpleIterate(A,threshhold,C,i+samples,sd,siglib(sig,:));
        for j=1:size(siglib)
            if(abs(ifft(A.*C)-siglib(j,:))<tolerance)
                flag=j;
                break;
            end
        end
        if(flag>0)
            break;
        end
    end
    samples=samples+i;
    time=toc;
end

Controller.m

function accepted = Controller(enteredpassword,sd)
%Tests whether or not a transmission of a password will activate the system
%This simulates the noise and processing as well as the values
    actualpassword=cat(1,13,5,10,4,2,8);
    accepted=1;
    redundancy=3;
    for i=1:size(actualpassword);
        flag=0;
        %while flag==0
        for j=1:redundancy
            [flag,runs]=testArbitrary(enteredpassword(i),sd);
        end
        if(flag~=actualpassword(i))
            accepted=-i;
            break;
        end
    end
end

Controller2.m

function Controller2()
%Helper function used to graph trends
    sig=1;
    for sd=0:30
        passed=0;
        for reps=1:50
            if(testArbitrary(sig,3+sd/10)==sig)
                passed=passed+1;
            end
        end
        temp(sd*10-29)=passed
    end
    subplot(1,1,1);
    plot(temp);
end

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks